/**
 * @author thanos
 */

	var autoplaySegment = '/autoplay';	
	if(autoplayTimeout == "autoplay")autoplayTimeout = "on";
	function goToNextPage(timerText, imageClicked){
		if(!$("ul.sf-menu li ul").is(":visible")){
			if($("#nav-next").length > 0){
				window.location = $("#nav-next").attr("href")+timerText;
			}else{
				if(imageClicked && timerText == ""){
					if($("#next-category-url").length > 0){
						window.location = $("#next-category-url").val();
					}else{
						window.location = $("#nav_categories li:first a").attr("href");
					}
				}else{
					window.location = $("#first-link").attr("href")+timerText;
				}
			}
		}else{
			autoplayTimeout = setTimeout("goToNextPage('"+timerText+"', false)", 3000);
		}
	}
	
	
	var sfTimeout = null;
	function hideSuperFishFn(){
		$('#crumbs a').removeClass("current");
         $('ul.sf-menu').find(":first").hideSuperfishUl();
	}
	function showMenuPath(){
        $('ul.sf-menu').find(":first").showSuperfishUl();
		var pathSegments = $('#crumbs a').html().split('/');
		var listString = "";
		for(var i=1;i<pathSegments.length;i++){
			for(var y=0; y<i; y++){
				listString+= " ul > li >";
			}
			listString = listString.substring(0, listString.length-2);
			var lists = $('ul.sf-menu > li >'+listString);
			lists.each(function(index){
				htmlText = $.trim($(this).find("a").html());
				var pathSegments = $('#crumbs a').html().split('/');
				if($.trim($(this).find("a").html().toLowerCase())==$.trim(pathSegments[i]).toLowerCase()){
					$(this).showSuperfishUl();
				 	$("> a", this).addClass("current");
				}
			});
			listString = '';
		}
	}
	// initialise plugins
	jQuery(function(){
		jQuery('ul.sf-menu').superfish( { 
            hoverClass:    'sfHover',       // the class applied to hovered list items 
            //pathClass:     'overideThisToUse', // the class you have applied to list items that lead to the current page 
            pathLevels:    1,                  // the number of levels of submenus that remain open or are restored using pathClass 
            delay:         1500,                // the delay in milliseconds that the mouse can remain outside a submenu without it closing 
            animation:     {'opacity':'show'},   // an object equivalent to first parameter of jQuery’s .animate() method 
            speed:         150,           // speed of the animation. Equivalent to second parameter of jQuery’s .animate() method 
            autoArrows:    false,               // if true, arrow mark-up generated automatically = cleaner source code at expense of initialisation performance 
            dropShadows:   false,               // completely disable drop shadows by setting this to false 
            disableHI:     false,              // set to true to disable hoverIntent detection 
            onInit:        function(){
                //DIXON HACK to show nav from a seperate parent element(not in the same list)
				 function showSuperfishUlFn(){
				 		$('#crumbs a').addClass("current");
						showMenuPath();
				 }
				 function setSfTimeoutFn(){
				 		sfTimeout = setTimeout("hideSuperFishFn()", 1500);
				 }
                 $('#crumbs').mouseover(showSuperfishUlFn ).mouseout(setSfTimeoutFn);
                 $('#logo').mouseover(showSuperfishUlFn).mouseout(setSfTimeoutFn);
		 		$('.sf-menu').mouseout(function(){
		 			$('#crumbs a').removeClass("current");
				});
				$('.sf-menu li a').mouseover(function(){
					$(this).parent().siblings().each(function(){
						$(this).hideSuperfishUl();
						$(this).children(":first").removeClass("current");
					})
				});
            },       // callback function fires once Superfish is initialised – 'this' is the containing ul 
            onBeforeShow:  function(){clearTimeout(sfTimeout);},       // callback function fires just before reveal animation begins – 'this' is the ul about to open 
            onShow:        function(){$(this).parent().children(":first").addClass("current");},       // callback function fires once reveal animation completed – 'this' is the opened ul 
            onHide:        function(){
				$(this).parent().children(":first").removeClass("current");
				}        // callback function fires after a sub-menu has closed – 'this' is the ul that just closed 
        });
        
        toggleTextFn = function(){
            $('#text').toggle();
			$(this).toggleClass("current");
		}
		
        $("#toggleText").click(toggleTextFn);
		$("#text-close-btn").click(toggleTextFn);
        
        
        $("#showText").click(
            function(){
                $('#text').toggle();
				$("#showThumbnails").toggleClass("current");
				$("#showText").toggleClass("current");
				return false;
            }
        );
        
        
        $("#showThumbnails").click(
            function(){
                $('#text').hide();
				$("#showText").removeClass("current");
				$("#showThumbnails").addClass("current");
				return false;
            }
        );
		
		function startPlayer(playBtnClicked){
				if ($("#nav-next").length == 0 && $("#nav-prev").length == 0){
					autoplayTimeout = 0;
					return;
				}
				$("#autoplay img").attr("src", $("#autoplay img").attr("src").replace("-play", "-pause"));
				if(playBtnClicked){
					goToNextPage(autoplaySegment, false)
				}else{
					autoplayTimeout = setTimeout("goToNextPage('"+autoplaySegment+"', false)", 3000);
				}
		}
		
		function stopPlayer(){
			window.location = document.location.href.replace(autoplaySegment, "");
		}
		
		if(autoplayTimeout == 'on'){
			autoplayTimeout = 0;
			startPlayer();
		}
		
		$("#autoplay").click(function(){
			if(autoplayTimeout == 0){
				startPlayer(true);
			}else{
				stopPlayer();
			}
			$("#autoplay img").blur();
			return false;
		});
		
		$("#photo a").click(function(){
			var timertext = (autoplayTimeout == 0) ? '' : autoplaySegment;
			goToNextPage(timertext, true);
			return false;
		});
		if($("#thumbs a.current").length > 0){
			$("#thumbs-close-btn a").attr("href", $("#thumbs a.current").attr("href"));
		}else{
			$("#thumbs-close-btn").hide();
		}
		$('#text a[class!="back"]').attr("target", "_blank");
		/*
		if($(window).height()<801){
			if($(window).height()< 801-25){
				$('html, body').animate({scrollTop:50},500);
			}else{
				$('html, body').animate({scrollTop:((801-$(window).height())/2)+25},500);
			}
		}*/
	});