var prUrl="http://www.tmgreece.com/dev/discoverevia.com";


$(document).ready(function() {
	
	$("span.menu").mouseover(function(){
		$(this).addClass("menu-active");
	});
	
	$("span.menu").mouseout(function(){
		$(this).removeClass("menu-active");
	});
	
});


var currentImage;
var currentIndex = -1;
var interval;
function showImage(index){
    if(index < $('#bigPic img').length){
        var indexImage = $('#bigPic img')[index]
        if(currentImage){   
            if(currentImage != indexImage ){
                $(currentImage).css('z-index',2);
                clearTimeout(myTimer);
                $(currentImage).fadeOut(250, function() {
                    myTimer = setTimeout("showNext()", 5000);
                    $(this).css({'display':'none','z-index':1})
                });
            }
        }
        $(indexImage).css({'display':'block', 'opacity':1});
        currentImage = indexImage;
        currentIndex = index;
        $('#thumbs li').removeClass('active');
        $($('#thumbs li')[index]).addClass('active');
        $('div.bigPic-text').removeClass('item-on').addClass('item-off');
        $('#featured-'+index).removeClass('item-off').addClass('item-on');
    }
}
 
function showNext(){
    var len = $('#bigPic img').length;
    var next = currentIndex < (len-1) ? currentIndex + 1 : 0;
    showImage(next);
}
 
var myTimer;
 
$(document).ready(function() {
	
	myTimer = setTimeout("showNext()", 5000);
	showNext(); //Load first image
	$('#thumbs li').bind('click',function(e){
		var count = $(this).attr('rel');
		showImage(parseInt(count)-1);
	});
	
	$("#photostory").jcarousel({
		wrap: "circular",
		visible: 3
	});
	
	
	/// Right column 4 tab module
	$("div.title-right-tab").click(function() {
		$("div.title-right-tab").removeClass("trt-on").addClass("trt-off");
		$(this).removeClass("trt-off").addClass("trt-on");
		$("div.title-right-tab").each(function(index) {
			if ( $(this).hasClass("trt-on") ) {
				$("#tabs-right-cont-ctrl").val(index);
			}
		});
		$("div.tabs-right-cont").each(function(index) {
			if ( index == $("#tabs-right-cont-ctrl").val() ) {
				$(this).show();
				var cont=$(this).attr("id").replace("cont-","");
				if ( $(this).html() == "" ) {
					$(this).html("<img src=\""+prUrl+"/images/loading.gif\" class=\"loading-r\" />");
					$.ajax({
						url: prUrl+"/ajax/getContent.php",
						data: "&type=rightarticles&cont="+cont,
						type: "GET",
						dataType: "text",
						timeout: 15000,
						cache: false,
						error: function(){
							//alert("Error loading XML document");
						},
						success: function(text){
							$("div.tabs-right-cont:eq("+index+")").html(text);
						}
					});
				}
			} else {
				$(this).hide();
			}
		});
	});
	/// Right column 4 tab module
	
	
	/// Position submenus
	$("a.menu").each(function(index) {
		var id=$(this).attr("id");
		$("#sub"+id).css({
					"left": $(this).offset().left - 3 + "px",
					"top": $(this).offset().top + 22 + "px"
				});
	});
	/// Position submenus
	
	
	/// open/close submenus
	$("a.menu").mouseover(function(){
		var id=$(this).attr("id").replace("menu-","");
		$("#submenu-"+id).show();
		$.doTimeout("submenus"+id);
	});
	$("div.submenu").mouseover(function(){
		var id=$(this).attr("id").replace("submenu-","");
		$("#menu-"+id).children("span.menu").addClass("menu-active");
		$(this).show();
		$.doTimeout("submenus"+id);
	});
	$("div.submenu").mouseout(function(){
		var id=$(this).attr("id").replace("submenu-","");
		$.doTimeout("submenus"+id,50,function(){
			$("#submenu-"+id).hide();
			$("#menu-"+id).children("span.menu").removeClass("menu-active");
		});
	});
	$("a.menu").mouseout(function(){
		var id=$(this).attr("id").replace("menu-","");
		$.doTimeout("submenus"+id,50,function(){
			$("#menu-"+id).children("span.menu").removeClass("menu-active");
			$("#submenu-"+id).hide();
		});	
	});
	/// open/close submenus
	
	
	/// font size
	$("img.fontUp").click(function() {
		var fontSize=$("div.article-maintext").css("font-size");
		fontSize = parseFloat(fontSize);
		if ( fontSize <= 17 ) {
			$("div.article-maintext").css("font-size",Number(fontSize+1));
		}
	});
	$("img.fontDown").click(function() {
		var fontSize=$("div.article-maintext").css("font-size");
		fontSize = parseFloat(fontSize);
		if ( fontSize >= 10 ) {
			$("div.article-maintext").css("font-size",Number(fontSize-1));
		}
	});
	/// font size
	
	
	/// toggle google map
	$("div.google-map-control").click(function(){
		if ( $(this).hasClass("gmc-close") ) { /// close the map
			$("#map_canvas").stop().animate({height:"0px"},"medium");
			$(this).removeClass("gmc-close").addClass("gmc-open");
			$("#google-map").css("padding-bottom","0px");
			$("div.google-map-directions").hide();
			$("#directions_panel").hide();
			$("#direction_details").hide();
			var ajAction=0;
		} else if ( $(this).hasClass("gmc-open") ) { /// open the map
			$("#map_canvas").stop().animate({height:"380px"},"medium");
			$(this).removeClass("gmc-open").addClass("gmc-close");
			$("#google-map").css("padding-bottom","10px");
			$("div.google-map-directions").show();
			$("#directions_panel").show();
			$("#direction_details").show();
			var ajAction=1;
		}
		$.ajax({
			url: prUrl+"/ajax/rememberGoogleMap.php",
			data: "&action="+ajAction,
			type: "GET",
			dataType: "text",
			timeout: 15000,
			cache: false,
			error: function(){
				//alert("Error loading XML document");
			},
			success: function(text){
				//
			}
		});
	});
	/// toggle google map
	
	/// Scroll page to content start when "More" link is hit on location description
	$("a.google-map-more").live("click",function() {
		$("html,body").animate({scrollTop: $("h1.article-title").offset().top},"slow");
	});
	/// Scroll page to content start when "More" link is hit on location description
	
	$("div.google-map-directions").live("click",function() {
		directionsDisplay.setMap(map);
		marker.setMap(null);
	});
});
