/*
* KWS Verkehrsmittelwerbung
* standard.js
*
* Author: Holger M. Stangl
* Company: Limeflavour | http://www.limeflavour.com
* Date: Oktober 2009
*/


	function showStep( id ){	
		$().ready(function() {
   			$('.step'+id).find("a").eq(0).click();
		});
	}

	function showAttribute( id ) {
    
		$step=$('#attribute_'+id);
		
		$('#slider2').stop()
		.animate({height:$step.height()+20},200)
		.scrollTo($step,600,{easing:'easeInOutExpo'});
        
        // ‹berblenden der Bilder
        /*$('.containerAttributeImages .imageBox').each(function() {
            $(this).fadeOut('fast').hide();
        });
        $('#attribute_image_'+id).fadeIn('slow').show();
        */
        // Navigation
        $('.navigationTabs li').each(function() {
            
            $(this).removeClass('active').removeClass('firstChildActive').removeClass('lastChildActive');
            
        });
        if ( $('#attribute_tab_'+id).hasClass('firstChild') ) {
            $('#attribute_tab_'+id).addClass('firstChildActive');
        } else if ( $('#attribute_tab_'+id).hasClass('lastChild') ) {
            $('#attribute_tab_'+id).addClass('lastChildActive');
        } else {
            $('#attribute_tab_'+id).addClass('active');
        }


    }
    
    function checkDownloadDialog( url, id, dialog ){
    
    	// ReadCookie
    	var downloadCookie = readCookie('kwsDownloadDialoge');
   		
   		if ( ( downloadCookie == 1 ) || ( dialog == 'N' ) ) {
   			document.location  = url+"index.php?action=download&id="+id;
   		} else {
   			//Download Dialog - open fancybox
    		$("#download"+id).fancybox().trigger('click');
   		}
   		
    }

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) 
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}


function loadProjectSelectBoxes() {
	
	$.getJSON(
		WEBROOT+"index.php", 
		{
			projectBranch: $('#projectBranch').attr('value'),
			projectAdmedium: $('#projectAdmedium').attr('value'),
			projectTarget: $('#projectTarget').attr('value'),
			projectAdtype: $('#projectAdtype').attr('value'),
			action: 'updateProjectQuickFinderAX',
			page: 10
		},
		function(data){
			//console.log(data);
			if ( data['admedium'] ) {
				$('.option:not(:first)', $('#projectAdmedium').prev() ).hide();
				$.each( data['admedium'], function(i, val){
					if ( val ) $('.'+val['Id'] , $('#projectAdmedium').prev() ).parent().show();
				});
			}

			if ( data['adtype'] ) {
				$('.option:not(:first)', $('#projectAdtype').prev() ).hide();
				$.each( data['adtype'], function(i, val){
					if ( val ) $('.'+val['Id'] , $('#projectAdtype').prev() ).parent().show();
				});
			}
			
			if ( data['target'] ) {
				$('.option:not(:first)', $('#projectTarget').prev() ).hide();
				$.each( data['target'], function(i, val){
					if ( val ) $('.'+val['Id'] , $('#projectTarget').prev() ).parent().show();
				});
			}
			
			if ( data['branch'] ) {
				$('.option:not(:first)', $('#projectBranch').prev() ).hide();
				$.each( data['branch'], function(i, val){
					if ( val ) $('.'+val['Id'] , $('#projectBranch').prev() ).parent().show();
				});
			}
			
		}
	);
	
}


function resetFilter() {
	$('.filterBox .selectBox .selectedBox').each(function() {
		$(this).html( $(this).next().find('.option:first').html() );
	});
	
	$('#projectBranch').attr('value','');
	$('#projectAdmedium').attr('value','');
	$('#projectTarget').attr('value','');
	$('#projectAdtype').attr('value','');
	
	loadProjectSelectBoxes();	
}


function pageload(hash) {
	if(hash) {
		// restore ajax loaded state
		if($.browser.msie) {
			hash = encodeURIComponent(hash);
		}
		$(".toggleTextBox a.toggle").trigger("toggleUp");
		$(".toggleTextBox a.toggle."+hash).trigger("toggleDown");
		
	} else {
		// start page
	}
}	
	
$().ready(function() {


	
	
	// PAGINATION 
	
	$(".paginate").each(function (){
		$(this).limePaginate({
				startPage: 0,
				pagerNumDisplay:2,
				pagerDotText:"<span>...</span>",
				itemsPerPage:5,

				lastPage:0,
				pager:'.paginationBox',
				items:'.itemBox',
				itemBox: '.teaserList',
				api:false,
				disabledClass: 'disabled',
				nextPage:'.next',
				prevPage:'.prev',
				pagerLinks: '.pages',
				pagerCurrentPage: '.currentPage',
				pagerTotalEntries: '.totalEntries',
				pagerTotalPages:'.totalPages',
				pagerAnchorBuilder:null,
				pagerAnchorJump:false,
				useHistroy:false
				
		});

		var apiPagi=$(this).limePaginate({api:true});
		var index=apiPagi.getItems().index($(".itemBoxActive"));
		apiPagi.setPage(Math.floor(index/5));
		
	});
	
	
	// Pagination Button Hover
	$('.paginationBox img').hover(
			function(){
				var newSrc = $(this).attr('src').replace("_act", "_hi");
				$(this).attr( 'src', newSrc );
			},
			function(){
				var newSrc = $(this).attr('src').replace("_hi", "_act");
				$(this).attr( 'src', newSrc );
			}
		);
	
	
	// SelectBox
	$('.selectBox').hoverIntent({
			sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
			interval: 100, // number = milliseconds for onMouseOver polling interval    
			over: nothing, // function = onMouseOver callback (REQUIRED)    
			timeout: 300, // number = milliseconds delay before onMouseOut    
			out: closeQuickBox // function = onMouseOut callback (REQUIRED)    
		}).click(function(){
			$(this).toggleClass('selectBoxActive');
		}).find('.option span').click( function() {
			var $thisClicked = $(this);
			var $thisSelectBox = $(this).parent().parent().parent();
			$thisClicked.parent().css()
			$thisSelectBox
				.find('.selectedBox span').text( $thisClicked.text() ).end()
				.find('input').attr('value', $(this).attr('class') );
		})
	function closeQuickBox(){
		$(this).removeClass('selectBoxActive');
	}
	function nothing(){ return true;}
	
	
	// QUICK SEARCH
	$('.filterBox .selectBox .option span').each(function() {	
		$(this).click( function() {
			
			if ( $(this).attr('class') != ""  ) {
				loadProjectSelectBoxes();
			} else {
				$('.filterBox .selectBox .option').each(function() {
					// Clear input field
					$(this).parent().next().attr('value','');
					// Clear selected box
					$(this).parent().prev().html( $(this).parent().find('.option:first').html() );
					// Set all options to display
					//$(this).show();													  
				});
				loadProjectSelectBoxes();
			}
		});
	});
	
	
	
	// TOGGLE MORE
	$('.toggleTextBox a.toggle').bind("toggleDown",function(){
		var $toggle= $(this).hide();
		$(this).prev().slideDown('fast', function(){
			$toggle.text('weniger').removeClass('more').addClass('less').show();
			$.scrollTo($(this).parent().parent().parent(),800);
		});
		return false;
	}).bind("toggleUp",function(e){
		var $toggle= $(this).hide();
		$(this).prev().slideUp('fast', function(){
			$toggle.text('mehr').removeClass('less').addClass('more').show();
		});
		return false;
		
	}).click(function(){
		if($(this).hasClass('less')){
			$(this).trigger("toggleUp");
			$.scrollTo($(this).parent().parent().parent(),800);
		} else {
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			$.historyLoad(hash);
		}
		return false;
		
	});
	
	$.historyInit(pageload);
	
	
	$("a.iframe").fancybox( {
		'hideOnContentClick': false,
		'zoomOpacity' : true,
		'overlayShow' : true,
		//'centerOnScroll' : false,
		'overlayOpacity' : 0.6,
		'padding' : 0,
		'frameWidth' : 560
	});
	
	
	$("a.popup").popup({
		width: 580,
		height: 680
		/*
		titlebar: [true/false],
		status: [true/false],
		resizable: [true/false],
		toolbar: [true/false],
		scrollbars: [true/false],
		menubar: [true/false]
		*/
	});

	
	
	$('.navigationA a').hover(
		function(){
			var oldSrc = $('img',this).attr( 'src' );
			$('img',this).attr( 'src', $(this).attr('rel') )
			$(this).attr('rel',oldSrc);
		},
		function(){
			var oldSrc = $('img',this).attr( 'src' );
			$('img',this).attr( 'src', $(this).attr('rel') );
			$(this).attr('rel',oldSrc);
		}
	);


	$('#socialMedia a').hover(
		function(){
			var oldSrc = $('img',this).attr( 'src' );
			$('img',this).attr( 'src', $(this).attr('rel') )
			$(this).attr('rel',oldSrc);
		},
		function(){
			var oldSrc = $('img',this).attr( 'src' );
			$('img',this).attr( 'src', $(this).attr('rel') );
			$(this).attr('rel',oldSrc);
		}
	);
	
	
	/*$('.teaserBox .textBox').each(function() {
		
		var $headline = $('h2 a',this);
		$('p a',this).hover(
			function(){ $headline.addClass( 'hover' ); },
			function(){ $headline.removeClass( 'hover' ); }
		);
	});*/
	
	$('.containerTeaser .teaserBox .textBox').each(function() {
		
		var $headlineBox = $(this).parent().parent();
		var $headline = $('h2 a',$headlineBox);
		var $paragraph = $('p a',this);
		
		$headline.hover(
			function(){ $headline.addClass( 'hover' );$paragraph.addClass( 'hover' ); },
			function(){ $headline.removeClass( 'hover' );$paragraph.removeClass( 'hover' ); }
		);
		$paragraph.hover(
			function(){ $headline.addClass( 'hover' ); },
			function(){ $headline.removeClass( 'hover' ); }
		);
	});
	
	$('.containerList .teaserBox .textBox').each(function() {
		
		var $headline = $('h2 a',this);
		var $paragraph = $('p a',this);
		
		$headline.hover(
			function(){ $headline.addClass( 'hover' );$paragraph.addClass( 'hover' ); },
			function(){ $headline.removeClass( 'hover' );$paragraph.removeClass( 'hover' ); }
		);
		$paragraph.hover(
			function(){ $headline.addClass( 'hover' ); },
			function(){ $headline.removeClass( 'hover' ); }
		);
	});
	
	
	
	$('.containerTeaserA .teaserBox .textBox').each(function() {
		
		var $container = $(this).parent().parent();
		var $textBox = $(this);
		var $headline = $('h3 a',this);
		
		$headline.click( function(){
			
			if ( $textBox.hasClass('textBoxOpen') )
			{
				$textBox.removeClass( 'textBoxOpen' );
			} else {
				$container.each(function() {
					$('.teaserBox .textBox',this).removeClass( 'textBoxOpen' );
				});
				$textBox.addClass( 'textBoxOpen' );
			}
		});
			
	});
	
	
	
	/*$('.teaserBox .textBox').each(function() {
		
		var $headline = $('h3 a',this);
		var $paragraph = $('p a',this);
		
		$headline.hover(
			function(){ $paragraph.addClass( 'hover' ); },
			function(){ $paragraph.removeClass( 'hover' ); }
		);
		$paragraph.hover(
			function(){ $headline.addClass( 'hover' ); },
			function(){ $headline.removeClass( 'hover' ); }
		);
	});*/
	
	
	
	// Pagination Button Hover
	$('.paginationBox img').hover(
		function(){
			var newSrc = $(this).attr('src').replace("_act", "_hi");
			$(this).attr( 'src', newSrc );
		},
		function(){
			var newSrc = $(this).attr('src').replace("_hi", "_act");
			$(this).attr( 'src', newSrc );
		}
	);
	
	// Search Button Hover
	$('#searchArrow').hover(
		function() {
			$(this).attr('src',WEBROOT+'files/static/images/gif/btn_search_hi.gif');					   
		},
		function() {
			$(this).attr('src',WEBROOT+'files/static/images/gif/btn_search.gif');				   
		}
	);
	
	
	// Search Field Hover
	$('#searchKey').focus(
		function() {
			if ( $(this).attr('value') == 'Suchbegriff eingeben' ) {
				$(this).attr('value','');	
				$(this).addClass('edit');
			}
		}
	);
	
	$('#searchKey').blur(
		function() {
			if ( $(this).attr('value') == '' ) {
				$(this).attr('value','Suchbegriff eingeben');
				$(this).removeClass('edit');
			}
		}
	);
	
	// MEDIA SLIDE
	$('.containerMedia').each(function() {

		var $mediaDetail = $('.imageBox',this);
		var $thumbBox = $('.thumbBox',this);

		$thumbBox.find('a').each(function() {

			$(this).click(function() {
				
				$('a',$thumbBox).each(function() {		   
					$(this).removeClass('active');
					$(this).parent().removeClass('thumbBoxActive');
					$(this).parent().find('p a').removeClass('active');
				});


				if($(this).attr('rel')){
				
					// ZOOM oder VIDEO	
					var rel= $(this).attr('rel');

					if(rel=="video"){
						// YOUTUBE
						var $player = $('<object width="525" height="273"><param name="movie" value="'+$(this).attr('href')+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+$(this).attr('href')+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="525" height="273"></embed></object>')
						.css('z-index', '101')
						.css('position', 'absolute')
						.hide()
						.appendTo($mediaDetail);

						$player.fadeIn('slow', function(){
							$($mediaDetail).children().eq(0).remove();
						});

						// ZOOM
						/*$flashBox.flash({
							swf: 'files/static/flash/zoom.swf',
							height: rel['screenHeight'],
							width: rel['screenWidth'],
							flashvars: {
								imgSrc: $(this).attr('href'),
								imgHeight: rel['screenHeight'],
								imgWidth: rel['screenWidth']
							},
							params: {
								menu: "false",
								wmode: "transparent"
							}

							});*/


						} else if (rel=="image"){
							// IMAGE
							var img = new Image();
							$(img)
							.load(function () {
								//$loader.fadeIn('fast');
								var $self=$(this)
								.hide()
								.css('z-index', '101')
								.css('position', 'absolute')
								.appendTo($mediaDetail);

								//$loader.fadeOut('fast');
								$self.fadeIn('slow', function(){
									$($mediaDetail).children().eq(0).remove();
								});

							})
							.error(function () { })
							.attr('src', $(this).attr('href'));
						}
					}


					//$mediaDetail.fadeOut('fast').attr('src',$(this).attr('rel') ).fadeIn('slow');
					$(this).addClass('active');
					$(this).parent().addClass('thumbBoxActive');
					$(this).parent().find('p a').addClass('active');

					// Display Text
					
					var $step = $(this).parent().parent().attr('class');

					$('.containerProjectDetail .textBox').each(function() {
						if ( $(this).attr('id') != $step ) $(this).hide();
					});
					$('#'+$step).fadeIn('slow');
					
					
					return false;

				});

				$('a.image',this).hover(
				function() {
					$(this).parent().find('p a').addClass('hover');
				},
				function() {
					$(this).parent().find('p a').removeClass('hover');
				}
			);
		
		});
		
	});
	
	// Thumb Image Slider
	$('.containerProjectMedia').each(function() {

		var $mediaDetail = $('.imageBox',this);
		var $thumbBox = $('.thumbBox',this);

		$thumbBox.each(function() {
		
			$('a.image',this).click(function() {
	
				$('a',$thumbBox).each(function() {		   
					$(this).removeClass('active');
					$(this).parent().removeClass('thumbBoxActive');
					$(this).parent().find('p a').removeClass('active');
				});


				if($(this).attr('rel')){
				
					// ZOOM oder VIDEO	
					var rel= $(this).attr('rel');

					if(rel=="video"){
						// YOUTUBE
						var $player = $('<object width="525" height="273"><param name="movie" value="'+$(this).attr('href')+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+$(this).attr('href')+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="525" height="273"></embed></object>')
						.css('z-index', '101')
						.css('position', 'absolute')
						.hide()
						.appendTo($mediaDetail);

						$player.fadeIn('slow', function(){
							$($mediaDetail).children().eq(0).remove();
						});

						// ZOOM
						/*$flashBox.flash({
							swf: 'files/static/flash/zoom.swf',
							height: rel['screenHeight'],
							width: rel['screenWidth'],
							flashvars: {
								imgSrc: $(this).attr('href'),
								imgHeight: rel['screenHeight'],
								imgWidth: rel['screenWidth']
							},
							params: {
								menu: "false",
								wmode: "transparent"
							}

							});*/


						} else if (rel=="image"){
							// IMAGE
							var img = new Image();
							$(img)
							.load(function () {
								//$loader.fadeIn('fast');
								var $self=$(this)
								.hide()
								.css('z-index', '101')
								.css('position', 'absolute')
								.appendTo($mediaDetail);

								//$loader.fadeOut('fast');
								$self.fadeIn('slow', function(){
									$($mediaDetail).children().eq(0).remove();
								});

							})
							.error(function () { })
							.attr('src', $(this).attr('href'));
						}
					}


					//$mediaDetail.fadeOut('fast').attr('src',$(this).attr('rel') ).fadeIn('slow');
					$(this).addClass('active');
					$(this).parent().addClass('thumbBoxActive');
					$(this).parent().find('p a').addClass('active');

					// Display Text
					
					var step = $(this).parent().parent().attr('class');
					var $step=$('#'+step);
					/*
					$('.containerProjectDetail .textBox').each(function() {
						if ( $(this).attr('id') != $step ) $(this).hide();
					});
					$('#'+$step).fadeIn('slow');
					*/
					//console.log($('#'+step).height());
					
					$('#slider1').stop()
					.animate({height:$step.height()},200)
					.scrollTo($step,600,{easing:'easeInOutExpo'});
					

					return false;

				});

				$('a.image',this).hover(
				function() {
					$(this).parent().find('p a').addClass('hover');
				},
				function() {
					$(this).parent().find('p a').removeClass('hover');
				}
			);
		
		});
		
	});

});
