/**
 * Rimini Berlin
 *
 * Copyright (c) 2009 Philipp Klein klein.philipp (at) gmail (dot) com
 *
 *
 */


var lang = 'de';
var view = 'categories'

$().ready(function() {
    $('.outer-panel').each(function(){ $(this).find('a.section-link:first').addClass("active");});
	$('.article-text a, .description a').attr('target','new');
	$().ajaxStart(function(){
	    $.fancybox.showActivity();
	});
	$().ajaxStop(function(){
	    $.fancybox.hideActivity();
	});
	fancyBoxParams = {
    				'titlePosition'		: 'inside',
    				'transitionIn'		: 'none',
    				'transitionOut'		: 'none',
    				'overlayOpacity'    : 0.8,
    				'overlayColor': "#000", 
    				'padding'           : 32
    			}
    // $(".playButton").fancybox(fancyBoxParams);
    			
	jQuery.each(jQuery.browser, function(i) {
      if($.browser.msie){

      }else{
        $('.category-inner-panel, .news-inner-panel').css('float','right');
      }
    });
    
	$(window).resize(function(){
		createNewStrip();
        moveStrips();
	});
    setTimeout("checkForStripSizeChange()",10);
	
	hideGeneral();
	hideArticle();
	
	$(".projekt").each(function(){
	    $(this).find('.img-count').text($(this).find('img').size())
	})
	
    $(".video").prev().addClass("videoPreview");
    // $('.playButton').bind('click', function(e){
    // 
    //     $(this).next().show();
    //     $(this).hide().prev().hide();
    //     return false;
    // })
	$('.link-category').hide();
	$('.link-category').bind('click',function(e){
	    $(".categories").load('get/categories',function(){
	        $('.categories .lang-' + lang).hide();
	        hideGeneral();
	        bindpic();
	        $('.link-category').hide();
    	    $('.link-year').show()
    	    $('.link-abc').show()
    	    view = 'categories';
	    })
	    return false;
	})

	$('.link-year').bind('click',function(e){
	    $(".categories").load('get/year',function(){
	        $('.categories .lang-' + lang).hide();
	        hideGeneral();
	        bindpic();        	
	        $('.link-year').hide();
    	    $('.link-category').show()
    	    $('.link-abc').show()
    	    view = 'year';
	    })
	    return false;
	})
	$('.link-abc').bind('click',function(e){
        
	    $(".categories").load('get/abc/'+lang,function(){
        	hideGeneral();
            bindpic();	
	    })
	    $('.link-abc').hide();
	    $('.link-category').show()
	    $('.link-year').show()
	    view = 'abc';
	    return false;
	})

	$('.link-lang-de').live('click', function(){
	    lang = 'de';
	    if(view == 'abc') {
	        $(".categories").load('get/abc/'+lang,function(){
    	        hideGeneral();
    	        bindpic();
    	        $('.lang-en').hide();
        		$('.lang-de').show();
    	    })
	    } else {
	        $('.lang-en').hide();
    		$('.lang-de').show();
	    }
		return false;
	})
	$('.link-lang-en').live('click', function(){
	    lang = 'en';
	    if(view == 'abc') {
	        $(".categories").load('get/abc/'+lang,function(){
    	        hideGeneral();
    	        bindpic();
    	        $('.lang-de').hide();
        		$('.lang-en').show();
    	    })
	    } else {
	        $('.lang-de').hide();
    		$('.lang-en').show();
    		
	    }
		return false;
	})
	
	

	$('.toggle-text').live('click',function(e){
		panel = $(".outer-panel").index($(this).closest(".outer-panel"));
		index = $(".outer-panel:eq("+panel+") .toggle-text").index($(this));
		$(".outer-panel").each(function(){
			$(this).find(".description:eq(" +index+ ")").toggle();
		});
		return false;
	})
	
	$('.article-next-link').live('click', function(e) {
	    sectionId = $(this).closest('.section').attr("class").split(' ')[1]
	    thisSection = $("."+sectionId)
	    visibleArticle = thisSection.find('.articles .article:visible');
	    if(visibleArticle.next().is('.article')) {
	        visibleArticle = visibleArticle.hide().next().show();
	    } else {
	        visibleArticle = visibleArticle.hide().parent().find('.article:first').show();
	    }
	    thisSection.find('.articles .article-navigate .num').text(thisSection.find('.articles .article').index(visibleArticle)+1)
	    return false;
	})
	
	$('.article-previous-link').live('click', function(e) {
	    sectionId = $(this).closest('.section').attr("class").split(' ')[1]
	    thisSection = $("."+sectionId)

	    visibleArticle = thisSection.find('.articles .article:visible');
	    if(visibleArticle.prev().is('.article')) {
	        visibleArticle = visibleArticle.hide().prev().show();
	    } else {
	        visibleArticle = visibleArticle.hide().parent().find('.article:last').show();
	    }
        thisSection.find('.articles .article-navigate .num').text(thisSection.find('.articles .article').index(visibleArticle)+1)
	    return false;
	})
	
	
	$('.next-link').live('click',function(e){
	    projectId = $(this).closest('.projekt').attr("class").split(' ')[1]
        projects = $("."+projectId);
		visibleImage = projects.find('.images .image:visible');
		visibleImage.hide();
		if(visibleImage.next().is('.image')){
			num = projects.find('.images .image').index(visibleImage.next());
			visibleImage.next().show();
			visibleImage.closest('.projekt').find('.navigate .num').text(++num);
		} else {
			visibleImage.parent().find('.image:first').show();
			visibleImage.closest('.projekt').find('.num').text("1");
		}
	
		visibleImage.closest(".projekt").find(".description").hide();

		return false;
	});

	$('.previous-link').live('click',function(e){
	    projectId = $(this).closest('.projekt').attr("class").split(' ')[1]
        projects = $("."+projectId);
		visibleImage = projects.find('.images .image:visible');
		visibleImage.hide();
		if(visibleImage.prev().is('.image')){
            // console.log("das")
			num = projects.find('.images .image').index(visibleImage.prev());
			visibleImage.prev().show();
			visibleImage.closest('.projekt').find('.navigate .num').text(++num);
		} else {
			visibleImage.parent().find('.image:last').show();
			count = $(visibleImage.closest('.projekt').find('.img-count')[0]).text();
			visibleImage.closest('.projekt').find('.num').text(count);
		}
		visibleImage.closest(".projekt").find(".description").hide();
		
		return false;
		
	});
	
	
	// Open/Close News
	$('a.section-link').click(function(){
	    sectionId = $(this).closest('.section').attr("class").split(' ')[1]
	    thisSection = $("."+sectionId)
	    
        outerPanel = thisSection.find(".news-outer-panel");
        innerPanel = outerPanel.find(".news-inner-panel");
        // console.log(outerPanel.get(0))
        if($(outerPanel.get(0)).width() <= 1) {
            thisSection.find("a.section-link").addClass("active");
            outerPanel.animate({
                width: $(innerPanel.get(0)).width()
            },300,function(){
                outerPanel.css({"width":"auto","overflow":"visible"})
            })
            // outerPanel.find('img:visible').animate({"width":"450px","height":"300px"});      
         
        } else {
             outerPanel.css("overflow","hidden").stop().animate({
                 width: "1px"
             },300)
            
             sectionLink = thisSection.find("a.section-link")
             index = $('.outer-panel:first .section-link').index($(sectionLink.get(0)))
             if(index > 0) sectionLink.removeClass("active");
        }
		return false;			
	})
	
	// Open/Close category
	$('a.category-link').live('click',function(){
		categoryId = $(this).closest('.category').attr("class").split(' ')[1]
		thisCategory = $("."+categoryId);
		thisCategory.find("a.category-link").addClass("active")
		outerPanel = thisCategory.find(".category-outer-panel")
		innerPanel = outerPanel.find(".category-inner-panel")
		if($(outerPanel[0]).width() <= 1) {
			outerPanel.animate({
		    	width: innerPanel.width()
			},300,function(){
                outerPanel.css({"width":"auto","overflow":"visible"})
			})
		} else {
			outerPanel.find(".name").hide()
			outerPanel.find(".name").hover(overPic,outPic)
			outerPanel.find(".navigate").hide()
			outerPanel.find('.playButton').hide();
			outerPanel.find(".projekt img:visible").animate({ 
			        width: "140px",
					height: "90px"
			      }, 300 ).closest('.images').hover(overPic,outPic).closest(".projekt").find(".description").hide();;	
			outerPanel
                .css("overflow","hidden")
                .stop()
			    .animate({
		    	width: "1px"
			},300)	
						
			thisCategory.find("a.category-link").removeClass("active")
		}
		return false;			
	})
	
	$('.name').live('click', function(e){
        $(this).closest('.projekt').find('.images').trigger('click');
	})
	
	// Enlarge/Small Projekt
	$('.projekt .images').live('click', function(e){
	    projectId = $(this).closest('.projekt').attr("class").split(' ')[1]
        projects = $("."+projectId);
		
		img = projects.find(".images").find(".image:visible img")
		if($(img[0]).width() <= 140) {
			img.animate({ 
		        width: "450px",
				height: "300px"
		      }, 300 ,function(){
		          img.closest(".images").find('.playButton').show();
		      });
			img.closest(".images").unbind("mouseenter mouseleave")
			    
			img.closest(".projekt").find(".name").unbind("mouseenter mouseleave").show()
			openNavigate(img.closest(".projekt").find(".navigate"));
            
		} else {
			img.stop().animate({ 
			        width: "140px",
					height: "90px"
			      }, 300 );
			img.closest('.images').hover(overPic,outPic)
			    .find('.playButton').hide();
			img.closest(".projekt").find(".name").hover(overPic,outPic).hide()
			img.closest(".projekt").find(".description").hide();
			closeNavigate(img.closest(".projekt").find(".navigate"))

		}
		return false;
	
	})
	// Hover show name
	bindpic();
	
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
        $('.link-lang-en').trigger('click');
        setTimeout(function(){
            $('.link-lang-de').trigger('click');
        }, 1000)
    }
	
})
function openNavigate(navi){
	// navi.find()
	navi.show()
}
function closeNavigate(navi) {
	navi.hide();
	navi.closest(".description").hide();
}

function hideGeneral(){
	// $('.projekt').hide()
	$(".playButton").fancybox(fancyBoxParams);
    $('.projekt .image').hide();	
	$('.images .image:first-child').show()
	$('.name').hide();
	$('.description').hide()
	$('.navigate').hide()
	resetImages();
	hideLanguage();
}

function hideArticle() {
	$('.article .image').show();
	$('.article').hide();
	$('.section .article:first-child').show();  
    // $('.section .article img').css({"width":"140px","height":"90px"});      
}

function resetImages() {
    $('.projekt .image:first-child img').css({"width":"140px","height":"90px"});

}

function hideLanguage() {
    if(lang == 'de') {
	    $('.lang-en').hide();
	    $('.lang-de').show()
	} else {
	    $('.lang-de').hide();
	    $('.lang-en').show()
	}
}
// function showProjekt(p){
//  p = $(p)
//  p.find(".images img").hide()
//  p.find(".images img:first").show()
// }

function overPic(e){
	$(e.target).closest('.projekt').find('.name').show();
}
function outPic(e){
	$(e.target).closest('.projekt').find('.name').hide();
}
function bindpic(){
	$('.projekt .images, .name')
		.hover(overPic,outPic)
}


// Smooth break

function checkForStripSizeChange(stripSize){
	thisStripSize = $(".inner-panel:first").width()
	if(thisStripSize != stripSize) {
		createNewStrip();
		moveStrips();
	}
	stripSize = $(".inner-panel:first").width() 
	setTimeout("checkForStripSizeChange("+thisStripSize+")",1000)
}


function createNewStrip(){
	$(".inner-panel:last").each(function(n){						
		innerPanel = $(this)
		innerPanelLeft = parseInt(innerPanel.css("left"))
		innerPanelWidth = parseInt(innerPanel.width())
		innerPanelVisiblePart = innerPanelWidth + innerPanelLeft
		windowWidth = parseInt($(window).width())
		if( (innerPanelVisiblePart > 0)) {
			outerPanel = innerPanel.parent();
			newPanel = outerPanel.clone(true)
			outerPanel.after(newPanel)
			moveStrips();
		} else {
			innerPanelBevore = innerPanel.closest(".outer-panel").prev().find(".inner-panel")
			innerPanelLeft = parseInt(innerPanelBevore.css("left"))
			innerPanelWidth = parseInt(innerPanelBevore.width())
			innerPanelVisiblePart = innerPanelWidth + innerPanelLeft
			if( (innerPanelVisiblePart <= 0)) {
				$(".outer-panel:last").remove()
			}
		}	
	})
}

function moveStrips(){
	$(".inner-panel:gt(0)").each(function(n){
		newLeft = parseInt($(window).width()) * (n+1)
		// console.log(newLeft)
		str = "-" + newLeft +"px";
        $(this).css({"left":str})
	})
}

