//javascript

$(document).ready(function () {
	
	//MAIN MENU ANIMATION (SCROLL WITH PAGE)
	$(window).scroll(
	function () {
		if  ($("#MainMenu").height() < $(window).height()) {
			var scrollAmount = $(document).scrollTop();
			if (scrollAmount > 182) $("#MainMenu").stop().animate({'top': scrollAmount + 'px', 'left': '0px'});
			else $("#MainMenu").stop().animate({'top': '182px', 'left': '0px'});
		}
		else $("#MainMenu").stop().animate({'top': '182px', 'left': '0px'});
	});
	
	//ANIMATION&TRIGGER FOR SLIDE PANELS
	$(".slide_trigger").click(
		function () {
			var elm = $(this);
			var panelState = false;
			var activePanel;
			$(".slide_box").each(
				function () {
					if ($(this).hasClass('active')) { 
						panelState = true;
						activePanel ="#"+$(this).attr("id");
					}
				}
			);
			if (panelState) {
				$(".slide_box").animate({left : "-100px"}, function () {$(".slide_box").removeClass('active');});
			}
			if (elm.attr("href") != activePanel) {
				$(elm.attr("href")).animate({left : "100%"}, 
					function () {
						$(elm.attr("href")).addClass('active');
						$('#leftSide, #content, #leftSide-Bg, #footer, #device_selection').bind('click', 
							function (e) {
								var target = $(e.target);
								if (!target.hasClass(".slide_box")) {
									$(".slide_box").animate({left : "-100px"}, function () {$('#leftSide, #content, #leftSide-Bg, #footer, #device_selection').unbind(); $(".slide_box").removeClass('active');});
								}
								else {}
							}
						)
					}
				);
			}
			return false;
		}
	);
	//CLOSING SLIDE PANEL
	$(".close_slide_box").click(
		function () {
			$(this).parent().animate({left : "-100px"}, function () {$('#leftSide, #content, #leftSide-Bg, #footer, #device_selection').unbind(); $(".slide_box").removeClass('active');});
		}
	);
	
	
	$(window).load( function () {
	//IMAGE GALLERY
	if($('.imageGallery').length != 0) {
		//Calculate width of scrolled area at page load
		$('.imageGallery .blocks').each(function () {
			var paneWidth = 0;
			$(this).children().each(function () {
				paneWidth = paneWidth + $(this).outerWidth() + 40;
				
				//align landscape images to middle
				if($(this).width() > $(this).height()) {
					$(this).css({
						"margin-top" : ($(this).parent().height() - $(this).height())/2 + 'px'
					});
				}
				
			});
			$(this).width(paneWidth + 1 + 'px');
			
			//hide left arrow if gallery to small
			if ($(this).width() < $(this).parent().width()) {
				$(this).parents('.imageGallery').find('.scrollLeft').addClass('hide');
			}
		});
		
		//Initialize the scroll script
		$('.scroll-pane').jScrollHorizontalPane({scrollbarMargin : 0, resize: false});
		
		//Make the gallery visible
		$('.scroll-pane').animate({opacity: '1'});
		
		//Resizing the gallery for liquid efect
		$(window).resize(function () {
			$('.jScrollPaneContainer').each(function () {
				$(this).width($(this).parent().width());
			});
			
			//show/hide left arrow when the window is resized
			$('.imageGallery .blocks').each(function () {
				if ($(this).width() < $(this).parent().width()) {
					$(this).parents('.imageGallery').find('.scrollLeft').addClass('hide');
				}
				else {$(this).parents('.imageGallery').find('.scrollLeft').removeClass('hide');}
			});
		});
		
		//Add left and right buttons functionality
		$('.scrollRight').addClass('hide'); // add class hide to the left arrow at page load
		
		$('.scrollLeft a').bind('click',function(){
			var thisPane = $(this).parent().parent().find(".scroll-pane");
			$(thisPane)[0].scrollBy(350);
			return false;
		});
		$('.scrollRight a').bind('click',function(){
			var thisPane = $(this).parent().parent().find(".scroll-pane");
			$(thisPane)[0].scrollBy(-350);
			return false;
		});
		
		//show/hide arrows if begining/ending of gallery reached
		$(".scroll-pane").each(function () {
			$(this).watch("left", function (data, i) {
				$('.scrollLeft').each(function () {
					var thisPane = $(this).parent().find(".scroll-pane");
					var el = $(this);
					var leftSide = el.parent().find(".jScrollPaneContainer").width() - el.parent().find(".blocks").width();
					if ($(thisPane).css("left") == leftSide + "px") el.addClass('hide');
					else el.removeClass('hide');
				});
				$('.scrollRight').each(function () {
					var thisPane = $(this).parent().find(".scroll-pane");
					var el = $(this);
					if ($(thisPane).css("left") == "0px") el.addClass('hide');
					else el.removeClass('hide');
				});
			}, 100);
		});
	}//end if - IMAGE GALLERY
	});
	
	//NEWSTIKER SLIDER
	$('#slider').infiniteNews({
		next: '#nextBtn',
		prev: '#prevBtn',
		width: 430
	});//end - NEWSTIKER SLIDER
	
	
	//LEFT MENU HEIGHT FIX
	$(window).load( function () {
		$('#leftSide-Bg').height($('#wrapper').height());
		$('#leftSide').height($('#wrapper').height());
	});//end - LEFT MENU HEIGHT FIX
});


/* FUNCTIONS */


