function slideSwitch() {
    var $active = $("#slideshow IMG.active");

    if ( $active.length == 0 ) $active = $("#slideshow IMG:last");

    var $next =  $active.next().length ? $active.next()
        : $("#slideshow IMG:first");

    $active.addClass("last-active");

    $next.css({opacity: 0.0})
        .addClass("active")
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass("active last-active");
        });
}
$(function() {
    setInterval( "slideSwitch()", 5000 );
});

$(document).ready(function() {
// get the lang var
	var lang = $("div.lang img").attr("src");
	lang = lang.substring(lang.length-6, lang.length - 4);

// simulated "remember" checkbox effect at the loginbox
	$("span.remember").click(function() {
		$(this).toggleClass("checked");
	});
// show vt
	$("a.vt").click(function(event) {
		event.preventDefault();
		var id = $(this).attr("id");
		var name = $(this).text();
		var dhn = $(this).attr("dhn");
		var dhl = $(this).attr("dhl");
		var num = $(this).attr("num");
		var date = $(this).attr("date");
		var booklink = $(this).attr("href");
        var place_url = "http://360darshan.com/" + lang + "/place/" + id;
		$("span.title").text(name);
        $("title").text("Free Virtual Tours to Holy India :: 360Darshan.com - " + name);
        $('meta[property|="og:title"]').attr("content", "Check out this " + name + " 360 virtual tour at 360darshan.com");
        $('meta[property|="og:url"]').attr("content", place_url);
		$("a.dhama-name").text(dhn);
		$("a.dhama-name").attr("href", dhl);
		$("span.num").text(num);
		$("span.date").text(date);
		$("a.readd").attr("href", booklink);
		$("a.send2f").attr("href", id);
		$("a.add2mf").attr("href", lang + "/myfaves/add/" + id);
		$("a.add2c").attr("href", lang + "/addtocart/" + id);
        $("div.social #fb").html("<script src=\"http://connect.facebook.net/en_US/all.js#xfbml=1\"></script><fb:like href=\"" + place_url + "\" layout=\"button_count\" ref=\"place_" + id + "\" show_faces=\"true\" width=\"450\"></fb:like>");
        $("div.social #tweet").html("<a href=\"http://twitter.com/share\" class=\"twitter-share-button\" data-count=\"horizontal\" data-text=\"Checking out this " + name + " 360 virtual tour at 360darshan.com\" data-url=\"" + place_url + "\" data-via=\"360darshan\" data-related=\"360darshan:related_account_description\">Tweet</a><script type=\"text/javascript\" src=\"http://platform.twitter.com/widgets.js\"></script>");
		document.player.execgui("loadpano(\"http://360darshan.com/" + lang + "/file/ivp/" + id + "\",false);");
		var href = $("#player").find("object[id|=aplayer]").find("param[name|=movie]").val();
		href = href.substring(0, 84) + id + "&as=1";
		$("#player").find("object[id|=aplayer]").find("param[name|=movie]").attr("value", href);
		$("#player").find("object[id|=aplayer]").find("embed[name|=aplayer]").attr("src", href);
	});
// login
	$(".submit-btn").click(function(event) {
		var str = $("span.remember").attr("class");
		var remember = "";
		if (str.search("checked")) remember = 1;
		else remember = 0;
		$.ajax({
		  type: "POST",
		  url: lang + "/main/login",
		  data: {   username: $("input.username").val(),
					password: $("input.password").val(),
					remember: remember
				},
		  dataType: "json",
		  success: function(data){
				$("div.loginbox").fadeOut("slow");
				$("div.login").html(data.loginbox);
				$("div.myfave ul.slided").html(data.myfave);
			}
		});
	});
	
// show the login box
	$("span.loginlink").click(function(event) {
		$("div.loginbox").fadeIn("slow");
	});
	
// exit image - fades out the login box
	$("img.loginimg").click(function() {
		$("div.loginbox").fadeOut("slow");
	});

// show the send to friend box
	$("a.send2f").click(function(event) {
		event.preventDefault();
        $("div.social").hide();
		var id = $(this).attr("href");
		$.ajax({
		  type: "POST",
		  url: lang + "/main/send2f",
		  data: {   id: id	},
		  dataType: "html",
		  success: function(data){
				$("div.send2f").prepend(data);
				$("div.send2f").fadeIn("slow");
			}
		});
	});
// cancel Send 2 Friend - fades out the box
	$("a.s2f-cancel").click(function(event) {
		event.preventDefault();
		$("div.send2f").fadeOut("slow").find("input").remove();
        $("div.social").show();
	});

// send 2 Friend - Submit
	$("a.s2f-submit").click(function(event) {
		event.preventDefault();
		$.ajax({
		  type: "POST",
		  url: lang + "/main/send2f",
		  data: {   name1: $("input.name1").val(),
					email1: $("input.email1").val(),
					name2: $("input.name2").val(),
					email2: $("input.email2").val(),
					message: $("textarea.message").val(),
					pid: $("div.send2f input:hidden").val()
				},
		  dataType: "json",
		  success: function(data){
				
                if (data.result)
                {
                    $("div.send2f").css("background-image", "url(../images/topdetbg.jpg)")
    				.html(data.html).delay(3000).fadeOut(1500).find("input").remove();
                }
                else
                {
                    $("div.send2f").find("input, textarea, span.link").remove();
                    $("div.send2f").prepend(data.html);                        
                }
			}
		});
	});
	
// the drop-downs
	
	$("span.dhamabox").click(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$("div.dhambox ul.slided").slideDown("fast").show(); //Drop down the subnav on click

		$("div.dhambox ul.slided").hover(function() {
		}, function(){	
			$("div.dhambox ul.slided").slideUp("slow"); //When the mouse hovers out of the subnav, move it back up
		});
	});
	$("span.myfave").click(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$("div.myfave ul.slided").slideDown("fast").show(); //Drop down the subnav on click

		$("div.myfave ul.slided").hover(function() {
		}, function(){	
			$("div.myfave ul.slided").slideUp("slow"); //When the mouse hovers out of the subnav, move it back up
		});
	});
	$("div.lang").click(function() { //When trigger is clicked...
		
		//Following events are applied to the subnav itself (moving subnav up and down)
		$("div.langs").slideToggle("fast").show(); //Drop down the subnav on click
		$("div.lang").toggleClass("ldown");
	});

// animate the flags

	$("div.langs a").hover(function(){
		var title = $(this).attr("title");
		$(this).find("img").animate({ 
			width: "80px",
			height: "50px"
			}, 500, "linear", function(){
				$(this).parent("a").parent("div").find("p").remove();
				$(this).parent("a").after("<p>" + title + "</p>");
				var index = $(this).parent("a").index("div.langs a");
				$(this).parent("a").parent("div").find("p").css("left", 5 + (index * 30) + "px");
			});
		},
		function(){
		$(this).parent().find("p").remove();
		$(this).find("img").animate({ 
			width: "20px",
			height: "12px"
			}, 500, "linear" );
	});
	$("div.langs").hover(function(){
		$(this).animate({ 
			width: "330px",
			height: "80px"
			}, 500, "linear" );
	},
	function(){
		$(this).animate({ 
			width: "270px",
			height: "22px"
		}, 500, "linear" );
	});
    $("div.langs a").click(function(event){
        var href = $(this).attr("href");
		var tung = href.substring(22, 24);
        if (tung == "es" || tung == "in" || tung == "cn" || tung == "de" || tung == "fr" || tung == "it" || tung == "pt" || tung == "jp")
        {
            event.preventDefault();
            window.location.replace("http://360darshan.com/" + lang + "/help/translate/" + tung);
        }
	});

});
