var currentState = 2;
function updateDeezerPlayer(newState) {
	if(newState == 1)
    {
        if(currentState == 2)
        {
            currentState = 1;
            jQuery('#gallery', window.parent.document).html("<embed id=\"deezer_player\" name=\"deezer_player\" width=\"226\" height=\"219\" allowfullscreen=\"true\" allowscriptaccess=\"always\" type=\"application/x-shockwave-flash\" src=\"http://www.deezer.com/embed/player?pid=30142358&amp;ap=0&amp;ln=fr\">");
        }
    }
    else if (newState == 2)
    {
        if(currentState == 1)
        {
            currentState = 2;
            jQuery('#gallery', window.parent.document).html("<embed id=\"deezer_player\" name=\"deezer_player\" width=\"226\" height=\"219\" allowfullscreen=\"true\" allowscriptaccess=\"always\" type=\"application/x-shockwave-flash\" src=\"http://www.deezer.com/embed/player?pid=30142358&amp;ap=1&amp;ln=fr\">");
        }
    }
}

function onYouTubePlayerReady(playerId) {
	if(!document.attachEvent)
    {
    	jQuery('embed').each(function() {
    		if(jQuery(this).attr('name') != 'deezer_player') 
    		{
    			jQuery(this)[0].addEventListener("onStateChange", "updateDeezerPlayer");
    		}
    	});
    }
}

jQuery(window).load(function(){
    // Manage gallery click
	var html = jQuery(".slideshow").html(); 
	jQuery(".slideshow").html("<a href=\"?page_id=102534\">"+html+"</a>");
});

jQuery(document).ready(function(){		
	// Manage AJAX request for mediakit codes
	function validCode() {
		jQuery('#mediakit_div p').each(function() {
			jQuery(this).hide();
		});
		var datas = 'code=' + jQuery('#codeValue').val();
		jQuery.ajax({
			   url: "/coton/mediakit/public/user/codelogin",
			   data: datas,
			   type: "post",
			   success: function(response) {
					response = eval('(' + response + ')');
					if(response['valid'])
					{
						jQuery('#codeIndic').show();
						jQuery('#rightFrame').attr('src', '/coton/mediakit/public/index/get-kits');
					}
					else
					{
						jQuery('#badCode').show();
					}
			   }
		});
	}
	
	jQuery('#codesubmit').click(validCode);
	
	jQuery('#codeValue').keypress(function(event) {
		if (event.keyCode == '13') {
			validCode();
		}
	});
	
	// Manage youtube and deezer player communication
	if(document.attachEvent)
	{
        var embeds = jQuery('.entry embed');
        if(embeds.length > 0)			// A video is present, shut donw deezer' player
        {
            jQuery('#gallery', window.parent.document).html("<embed id=\"deezer_player\" name=\"deezer_player\" width=\"226\" height=\"219\" allowfullscreen=\"true\" allowscriptaccess=\"always\" type=\"application/x-shockwave-flash\" src=\"http://www.deezer.com/embed/player?pid=30142358&amp;ap=0&amp;ln=fr\">");
        }
        else							// No video, check if player should be restart
        {
        	var deezer = jQuery('#deezer_player', window.parent.document).attr('src');
        	if(deezer && deezer.lastIndexOf("ap=1") == -1)
    		{
                jQuery('#gallery', window.parent.document).html("<embed id=\"deezer_player\" name=\"deezer_player\" width=\"226\" height=\"219\" allowfullscreen=\"true\" allowscriptaccess=\"always\" type=\"application/x-shockwave-flash\" src=\"http://www.deezer.com/embed/player?pid=30142358&amp;ap=1&amp;ln=fr\">");
    		}
        }
	}
	
	jQuery('#mediakit_h2').toggle(
			function () {
				jQuery('#mediakit_div').fadeIn();
			},
			function () {
				jQuery('#mediakit_div').fadeOut();
			}
	);	
	
	jQuery("a.more-link").each(function() {
		jQuery(this).before('<br />');
	});
	
	// Manage display
	jQuery(".slideshow").css("margin-top", "5px");
	jQuery(".slideshow").css("margin-left", "-20px"); 
	
	// Delete comments for send to friend
	chaine = location.search;
	if(chaine.lastIndexOf("&email=") != -1)
	{
		jQuery('#comments').css('display', 'none');
		jQuery('.commentlist').css('display', 'none');
		jQuery('#navigation').css('display', 'none');
		jQuery('#respond').css('display', 'none');
	}
	
	// Gestion frame
	// Old
	chaine = top.location.search;
	if((chaine.lastIndexOf("&p=") != -1 || chaine.lastIndexOf("?p=") != -1) 
			&& chaine.lastIndexOf("preview=true") == -1)
	{
		if(chaine.lastIndexOf("redirect=ok") == -1)
		{
			// Pade id
			pageId = chaine.substring(chaine.lastIndexOf("p=")+2);
			window.location.replace("?pageFrame="+pageId+"&redirect=ok");
		}
	}
	// New
	newChaine = top.location;
	host = "http://"+window.location.host;
	if(newChaine != host && newChaine != host+'/' && newChaine != 'http://localhost/coton/') 
	{	
		jQuery.ajax({
	      url: "../../wp-content/themes/default/redirect.php",
	      type: "POST",
	      data: "url="+newChaine,
	      success: function(msg)
	      {
			window.location.replace('/');
		  }
		});
	}
	
	// Manage bloken url
	jQuery("img:regex(src, ^/http)").each(function() {
		var src = jQuery(this).attr('src');
		src = src.replace('/http', 'http');
		jQuery(this).attr('src', src);
	});
});

function resizeFrame() {
    jQuery("#rightFrame").height(jQuery("#rightFrame").contents().find("body #content").outerHeight(true) + 20);
    jQuery("#rightFrame").width(555);

    window.scrollTo(0,0);
    self.scrollTo(0,0);

    var ifr = document.getElementById('rightFrame').contentDocument;
    if (typeof ifr == "undefined")
        ifr = document.getElementById('rightFrame').contentWindow.document;

    var l = ifr.location.href;
    var p = l.indexOf('#');
    if (p != -1)
    {
        var id = l.substring(p);
        window.scrollTo(0, jQuery(ifr).find(id).position().top);
    }
}

