/*----------------------------------------------------------------------------//
// Javascript pour le site client  -------------------------------------------//
//----------------------------------------------------------------------------//
// Client : -
// Note   : -
// Date   : -
//----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
// Inclusion dynamique des fichiers externes javascripts
/*----------------------------------------------------------------------------*/
function importJavascript(src) {
	document.write("<script type=\"text/javascript\" src=\"" + src + "\"></scr" + "ipt>");
}
function importCss(href, media) {
	document.write('<link rel="stylesheet" type="text/css" media="' + media + '" href="' + href + '" />');
}

function RenderClick(e, btnID, validationGroup) {
    if (window.event) {// IE
        keynum = e.keyCode;
    } else if (e.which) {// Netscape/Firefox/Opera
        keynum = e.which;
    }
    // Si Enter
    if (keynum == 13) {
        // Lancer un postback sur le bouton passé en paramètre
        if (Page_ClientValidate(validationGroup)) {
            __doPostBack(btnID, '');
        }
        return false;
    }
    return true;
}


/*----------------------------------------------------------------------------*/
// Importations de fichiers
/*----------------------------------------------------------------------------*/
// importJavascript("/includes/js/plugins/jquery.plugin.js");
//importCss("../includes/js/plugins/thickbox.css", "screen");
importJavascript("/includes/js/plugins/watermark.js");
importJavascript("/includes/abso.navigateur.js");
importJavascript("/includes/js/plugins/thickbox.webtele.js");
importCss("http://www.canalvie.com/_css/thickbox_webtele.css", "screen");

/*----------------------------------------------------------------------------*/
// Declarations jQuery
/*----------------------------------------------------------------------------*/
jQuery(document).ready(function() {

	InitOldBrowser();
	jQuery("#Contenu hr").replaceWith('<div class="HR"></div>');
	jQuery("#ColD .AbonnerInfolettre input:text").Watermark("Votre courriel", "#bab3af");

	//Header Canal Vie
	if ( jQuery.cookie('cv_menu') == null ) {
		jQuery("#header_menu_bloc").css("height", "58px");
		jQuery("#header_tab a").addClass("On");
		jQuery.cookie('cv_menu', 'close', { expires: 1 });
	} else {
		jQuery("#header_menu_bloc").css("height", "24px");
		jQuery("#header_tab a").removeClass("On");
	}
		
	/*if ( jQuery.cookie('cv_menu') == 'close' ) {
		jQuery("#header_menu_bloc").css("height", "24px");
		jQuery("#header_tab a").removeClass("On");
	} else {
		jQuery("#header_menu_bloc").css("height", "58px");
		jQuery("#header_tab a").addClass("On");
	}*/

	jQuery("#header_tab a").click(function(){
		if (!jQuery(this).hasClass("On")) {
			jQuery("#header_menu_bloc").css("height", "58px");
			jQuery(this).addClass("On");
			//jQuery.cookie('cv_menu', 'open', { expires: 1 });
		} else {
			jQuery("#header_menu_bloc").css("height", "24px");
			jQuery(this).removeClass("On");
			//jQuery.cookie('cv_menu', 'close', { expires: 1 });
		}
	});

    //ColD, Capsules
    jQuery("#ColD .Capsules .Nav a").click(function() {
        var currParent = jQuery(this).parents(".Capsules");
        jQuery(".Nav a", currParent).removeClass("On");
        jQuery(".Tab", currParent).hide();
        jQuery(this).addClass("On");
        jQuery(".Tab." + jQuery(this).attr("href").substring(1), currParent).show();
        return false;
    });

    //Canal Vide de chien, Promos
	VDCPromos = jQuery("a", jQuery("#VieDeChienPromo .Nav").children("li:not(.Btn)"));
	maxVDHPromo = VDCPromos.length - 1;
    jQuery("#VieDeChienPromo .Nav").children("li:not(.Btn)").each(function() {
		jQuery("a", this).click(function(){
			setVDCPromo(this);
			return false;
		});
    });
    jQuery("a", jQuery("#VieDeChienPromo .Nav").children("li:first")).click(function(){
		if ( currVDCPromo == 0 ) {
			currVDCPromo = maxVDHPromo;
		} else {
			currVDCPromo--;
		}
		setVDCPromo( VDCPromos[currVDCPromo] );
	});
	jQuery("a", jQuery("#VieDeChienPromo .Nav").children("li:last")).click(function(){
		if ( currVDCPromo == maxVDHPromo) {
			currVDCPromo = 0;
		} else {
			currVDCPromo++;
		}
		setVDCPromo( VDCPromos[currVDCPromo] );
    });
	setVDCPromo( VDCPromos[currVDCPromo] );

    //VideoPage
    jQuery("#VideoPage a").click(function() {
        jQuery("#VideoPage .Apercu, #VideoPage .Video").toggle();
        return false;
    });

    //Bonnes Adresses
    jQuery("#AdressesRecherche .Recherche").css("display") == "none" ? jQuery("#AdressesRecherche h2 span").text("+") : jQuery("#AdressesRecherche h2 span").text("-");
    jQuery("#AdressesRecherche h2 a").click(function() {
        jQuery("span", this).text() == "+" ? jQuery("span", this).text("-") : jQuery("span", this).text("+");
        jQuery("#AdressesRecherche .Recherche, #AdressesRecherche .Bas").toggle();
    });
    jQuery("#AdressesTabs a").click(function() {
        jQuery("#AdressesTabs li").removeClass("On");
        jQuery("#AdressesCarte, #AdressesListe").hide();
        jQuery(this).parents("li").addClass("On");
        jQuery(jQuery(this).attr("href")).show();
        return false;
    });

    //Rating
    InitRating();

});

// -- Login ---------------------------------------------------------------
function showLoggedIn() {
    if (isCanalVieLogged)
        jQuery("#headerLoggedInBox, #headerLoginBox").toggle();
}

// -- Saut de champ texte pour téléphone, code postal, etc... -------------
function jumpToNext(self) {
	if ( jQuery(self).val().length >= parseInt(jQuery(self).attr("maxlength")) )
		jQuery(self).next("input:text").focus();
}

// -- Promos Canal Vie de chein -------------------------------------------
var VDCPromos = [];
var maxVDCPromo = 0;
var currVDCPromo = 0;
function setVDCPromo(currA) {
	jQuery("#VieDeChienPromo .Nav a").removeClass("On");
	jQuery(currA).addClass("On");
	jQuery("#VieDeChienPromo .Texte a").attr("href", jQuery(currA).attr("href"));
	jQuery("#VieDeChienPromo .Texte h2 a").text(jQuery(currA).next("ul").find("li:nth-child(1)").text());
	jQuery("#VieDeChienPromo .Texte p:first").text(jQuery(currA).next("ul").find("li:nth-child(2)").text());
	jQuery("#VieDeChienPromo .Photo img").attr("src", jQuery(currA).next("ul").find("li:nth-child(3)").text());
	jQuery("#VieDeChienPromo .Texte h2, #VieDeChienPromo .Texte p").show();
}

// -- Vote par patte  -----------------------------------------------------
function InitRating() {
	if ( isCanalVieLogged ) {
		jQuery(".Rating:not(.Static)").each(function() {
			var self = jQuery(this);
			var vote = 0;
			
			var ajaxUrl = self.hasClass("VieDeChien") ? "/Tools/CanalVieDeChienSubmissionVote.aspx" : "/Tools/GoodAddressesVote.aspx";
			var ajaxSuccess = function(msg) {
				jQuery(".Vote", self).remove();
				if ( jQuery(".Nmb", self).length == 0 )
					self.append('<span class="Nmb"></span>');
				jQuery(".Nmb", self).text("Merci!");
			};
			var ajaxError = function(msg) {
				alert(msg.error);
			};
			
			var voteHtml = '<span class="Vote">';
			for (x = 1; x <= 5; x++) { voteHtml += '<a href="javascript:void(0);"></a>'; }
			voteHtml += "</span>";
			self.prepend(voteHtml);
			
			jQuery(".Vote a", self).hover(function() {
				var curr = jQuery(".Vote a", self);
				vote = (curr.index(jQuery(this)) + 1);
				jQuery(".Total", self).css("width", (vote * 20) + "%");
			}, function() {
				vote = 0;
				jQuery(".Total", self).css("width", jQuery(".Total", self).text());
			})
			
			jQuery(".Vote a", self).click(function() {
				if ( self.hasClass("Input") ) {
					jQuery(".Total", self).text((vote * 20) + "%");
					jQuery("input[id$='hdRankForInput']", self).val(vote);
				} else {
					jQuery.ajax({
						type: "GET",
						url: ajaxUrl,
						data: "NodeId=" + jQuery("input:hidden", self).val() + "&Vote=" + vote,
						success: ajaxSuccess,
						error: ajaxError
					});
				}
			});
		});
	}
}

// ***************************************************** Ad tag generation script *****************************************************
function trim(toTrim) {
	return toTrim.replace(/^\s+/g,'').replace(/\s+$/g,'');
}
function endsWith(str, endStr) {
	var originalStringWithEndStrInserted = str.substring(0, str.length - endStr.length) + endStr;
	if (str == originalStringWithEndStrInserted) {
		return true;
	} else {
		return false;
	}
}


function callWS(url, data, success) {
    $.ajax({
        url: url,
        data: data,
        type: "POST",
        processData: true,
        contentType: "application/json; charset=utf-8",
        timeout: 10000,
        dataType: "json",
        success: success,
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            //alert(XMLHttpRequest.responseText);
        }
    });
}

function getJSONParam(key, value) {
    var paramFormat = '"{0}": "{1}"';
    return paramFormat.format(key, value);
}

