/* config */
var _ajax_call_url = "/ajax/Default.aspx";

/* generic functions */
$(function() {
	function addMega(){ 
		$($(this).find("div")).slideDown("fast"); $(this).addClass("hovering"); 
	} 

    function removeMega(){
	    $($(this).find("div")).slideUp("fast");
        $(this).removeClass("hovering");
    }

    var megaConfig = {
         interval: 5,
         sensitivity: 7,
         over: addMega,
         timeout: 200,
         out: removeMega
    };

    $("li.mega").hoverIntent(megaConfig)


		/* login popup */
	/*	$('#login_link').click(function(){
			$('.login_div').fadeIn('normal');
			
			
		});*/
	$("#login_link").click(function(){
				if ($(".login_div").is(':hidden'))
		                 $(".login_div").show();
		             else{
		                 $(".login_div").hide();
		     }
		             return false;
	});
		
    $('.login_div').click(function(e) {
        e.stopPropagation();
    });
    $(document).click(function() {
        $('.login_div').hide();
    });
	
    rollOverImage_gen();
});




function rollOverImage_gen(){
	
var images_to_switch=document.getElementsByTagName('img');

	 	for(i=0;i<images_to_switch.length;i++)
	 	{
	  		if(/img_to_switch/.test(images_to_switch[i].className)){
					images_to_switch[i].onmouseover = function(){switchImage(this);};
					images_to_switch[i].onmouseout = function(){switchBackImage(this);};
			}
		}
}

function switchImage(o){
	var src,ftype,newsrc;
		
	 	src = o.src;
		
	 	ftype = src.substring(src.lastIndexOf('.'), src.length);

		if(/_hover/.test(src) == false)
		 {
		  	newsrc = src.replace(ftype, '_hover'+ftype);
				o.src = newsrc;
}
}

function switchBackImage(o){

	var src,ftype,newsrc;

	 	src = o.src;
	ftype = src.substring(src.lastIndexOf('.'), src.length);
		if(/_hover/.test(src))
		 {
		  	newsrc = src.replace('_hover','');
			 	o.src = newsrc;
		}
}

/* to close the login popup */
function close_me(){
		$('.login_div').fadeOut('normal');
}

//// browser detection
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


// wishlist methods
function addWish(lang)
{
  var id=$('#productid').val();
  var colorid=$('#colorid').val();
  var sizeid=$('#sizeid').val();
  var msg=$('#msg').val();
  
  $.ajax({
    type: "POST",
    url: _ajax_call_url,
    data: {action:"addWish",id: id,colorid: colorid,sizeid: sizeid,msg: msg,language:lang,callback:'addWishCallBack'},
    dataType: "xml",
    success: parseAjaxResponse
  });
 
}

function addWishCallBack(r)
{
    if (r.status!='OK') return; 
    $('#wishlist_items').html(r.content);
    $('#btn_view_wishlist').show();
}

function removeWish(lang,id,colorid,sizeid)
{
  $.ajax({
    type: "POST",
    url: _ajax_call_url,
    data: {action:"delWish",id: id,colorid: colorid,sizeid: sizeid,language:lang,callback:'removeWishCallBack'},
    dataType: "xml",
    success: parseAjaxResponse
  });
}


function removeWishCallBack(r)
{
    if (r.status!='OK') return; 
    if (r.nextAction=='close') // no more items
    {
        $.fancybox.close();
        $('#btn_view_wishlist').hide();
        $('#wishlist_items').html('');
        $('#wishlist_empty').show();
    }
    else $('#'+r.content).remove();
}


function wishListInit(lang)
{
    wishListProducts();
    
    $.ajax({
    type: "POST",
    url: _ajax_call_url,
    data: {action:"wishlist",language:lang,callback:'wishListInitCallBack'},
    dataType: "xml",
    success: parseAjaxResponse
  });
}

function wishListInitCallBack(r)
{
    if (r.status!='OK') return; 
    $('#wishlist_items').html(r.content);
}

function  wishListProducts(lang)
{
    $('#wishlist_form').hide();
    $('#wishlist_send').hide();
    $('#wishlist_products').show();
}
function  wishListForm(lang)
{
    $('#wishlist_products').hide();
    $('#wishlist_send').hide();
    $('#wishlist_form').show();
    validateWishListForm();
}
function  wishListSend(lang)
{
  if (!$("#whishListSendForm").valid()) return;
  
  var formdata="action=sendWishlist&language="+lang+"&callback=wishListSendCallBack&"+$("#whishListSendForm").serialize();
  
  $.ajax({
    type: "POST",
    url: _ajax_call_url,
    data: formdata,
    dataType: "xml",
    success: parseAjaxResponse
  });
  
}

function wishListSendCallBack(r)
{
    // reset the form
    var nbfriends=parseInt($('#nbfriends').attr('value'));
    $("#whishListSendForm")[0].reset();
    $('#nbfriends').attr('value',nbfriends);
    
    // show the confirmation page
    $('#wishlist_products').hide();
    $('#wishlist_form').hide();
    $('#wishlist_send').show();
    $('#wishlist_items').html('');
    $('#btn_view_wishlist').hide();
    $('#wishlist_url').attr('href',r.content);
}

function wishListAddFriend_en()
{
    var nbfriends=parseInt($('#nbfriends').attr('value'));
    nbfriends=nbfriends+1;
    
    $('#friendnames').append("<p><label>Friend's Name</label> <input type='text' id='toname"+nbfriends+"' name='toname"+nbfriends+"' maxlength='50'/></p>");
    $('#friendemails').append("<p><label>Friend's Email</label> <input type='text' id='toemail"+nbfriends+"' name='toemail"+nbfriends+"' maxlength='50'/></p>");
    
    $('#nbfriends').attr('value',nbfriends);
}

function wishListAddFriend_fr()
{
    var nbfriends=parseInt($('#nbfriends').attr('value'));
    nbfriends=nbfriends+1;
    
    $('#friendnames').append("<p><label>Nom de votre ami(e)</label> <input type='text' id='toname"+nbfriends+"' name='toname"+nbfriends+"' maxlength='50'/></p>");
    $('#friendemails').append("<p><label>Courriel de votre ami(e)</label> <input type='text' id='toemail"+nbfriends+"' name='toemail"+nbfriends+"' maxlength='50'/></p>");
    
    $('#nbfriends').attr('value',nbfriends);
}

