var current_back  = "none";
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
var background;
var imageZoom;
var closeButton;
var favorite;
var flag_special_items=0;
var last_special_items= "";
var nb_special=0;
var piccode;
var functions = {	
    // Lancement de l'appel AJAX si on clique sur les montres
	clickOnBox: function(){
		$(".super_bloc > div.bloc").bind('click', function() {
			$(this).attr("alt","clicked");
			functions.appelAjax($(this));
		});
	},
	
	// Lancement de l'appel AJAX si on clique sur le zoom
	clickOnZoom: function(){
		$(".zoom > div.bigzoom").bind('click', function() {
			$(this).attr("alt","clicked");
			functions.showZoom($(this));
		});
		$("#product_visual > div#image_ref").bind('click', function() {
			$(this).attr("alt","clicked");
			functions.showZoom($(this));
		});
	},
	
	clickToHide: function() {
       $("div#bloc_zoom > div#content_zoom").bind('click', function() {
            $(this).attr("alt","clicked");
            functions.hideZoom($(this));
       });
       $("div#bloc_zoom > div#close_zoom").bind('click', function() {
            $(this).attr("alt","clicked");
            functions.hideZoom($(this));
       });
    },
    
    
    // --------------- Pour les favoris -----------------------
    clickOnFavorite: function() {
       $(".favorite > div.favlink").bind('click', function() {
            $(this).attr("alt","clicked");
            functions.showFavorite($(this));
       });
    },
    
    clickToHideFavorite: function() {
       $("div#bloc_favorite > div#close").bind('click', function() {
            $(this).attr("alt","clicked");
            functions.hideFavorite($(this));
       });
    },
	//-----------------------------------------------------------
	
	// --------------- Pour les collections specials -----------------------
    clickOnSpecial: function() {
       $("#icons_special > img").bind('click', function() {
            $(this).attr("alt","clicked");
            functions.showSpecial($(this));
       });
    },
    
    clickToHideFavorite: function() {
       $("div#bloc_favorite > div#close").bind('click', function() {
            $(this).attr("alt","clicked");
            functions.hideFavorite($(this));
       });
    },
	//-----------------------------------------------------------
	
	// Appel AJAX
	appelAjax: function(my_bloc){
	    var my_id = my_bloc.parent().attr("id");
		var my_short_id = my_bloc.parent().attr("idr"); // Modif Fred 14.04.2009 17:34: retourne un id du nom de l'ambassadeur, pas l'index de l'id 
		//var my_short_id = my_id.replace("bloc",""); // Modif Fred 14.04.2009 17:34: donc plus besoin
		var my_fam_id = my_bloc.parent().attr("idf"); //Modif Céd 05.05.2009. Retourne l'id de la famille
		var my_col_id = my_bloc.parent().attr("idc"); //Modif Céd 05.05.2009. Retourne l'id de la collection
        piccode = my_short_id;
        
        $.ajax({
			type: 'POST',
			url: '?ajax_collections',
			data: 'name=' + my_short_id + '&action=getreference&colid=' + my_col_id + '&famid=' + my_fam_id + '&refid=' + my_short_id,
			beforeSend: function() {
				$("div#collection_bloc").hide();
				//$("div#bloc_left").html('<img src="_img/general/loader_sans_gif.jpg" />');
			},
			success: function(msg) {
				//var my_index = $("div#bloc_right > div").index(my_bloc.parent());
				//$("div.listNav > ul > li").children("a").addClass("puce_nav").removeClass("puce_nav_on");
				//$("div.listNav > ul > li").eq(my_index).children("a").addClass("puce_nav_on");
				
				$("div#collection_content").html(msg);
				$("div#collection_content").show();
				var arVersion = navigator.appVersion.split("MSIE");
				var version = parseFloat(arVersion[1]);
				if ((version >= 5.5)) {
					$(".super_bloc > div.bloc[alt!=over]").css("filter","");
					$(".super_bloc > div.bloc[alt!=over]").css("background-image","none");
				} else {
					$(".super_bloc > div.bloc[alt!=over]").css("background-image","none");
				}
				if ((version >= 5.5)) {
					my_bloc.css("filter","progid:DXImageTransform.Microsoft.AlphaImageLoader(src='pages/_img/ambassadors/home/fd_" + my_id + "_on.png', sizingMethod='image')");
				} else {
					my_bloc.css("background-image","url(pages/_img/collections/home/fd_" + my_id + "_on.png)");
				}
				
				//Masque le bloc des specials dans le cas ou il serait ouvert.
				functions.hideSpecial();
				
				
                /*
                //Ré-écriture de l'url
                var path = window.location.href;
				var len = window.location.host.length;
				var pos = path.indexOf('/refid_', 1);
				
				var newPath = path.substr(0, pos);
				
				var refPath = '/refid_'+my_short_id;
				
				var finalPath = newPath+refPath;
				//alert(finalPath);
				
				location.replace(finalPath);*/
                
				
				//$("div#bloc_left").fadeIn();
			}
		});
	},
	

	// Appel AJAX pour le zoom
	showZoom: function(my_bloc){
	   var refid = my_bloc.parent().attr("idr");

       $.ajax({
			type: 'POST',
			url: '?ajax_collections',
			data: 'name=' + refid + '&action=showZoom&refid=' + refid,
			beforeSend: function() {

			},
			success: function(msg) {
				/*$("div#content_zoom").html(msg);
				$("div#content_zoom").show();
				$("div#content_zoom").empty();
				$("div#content_zoom").append(msg);*/

                background = jQuery("<div id='background_items'></div>").css({opacity: 0.6}).appendTo(document.body);
                imageZoom = jQuery("<div id='bloc_zoom'><div id='close_zoom' class='close'><img src='pages/_img/collections/cross.gif' /></div><div id='content_zoom'>"
                +msg+"</div></div>").css({opacity: 1}).appendTo(document.body);
                //closeButton = jQuery("<div id='close' class='close'><div id='close_favorite'><img src='pages/_img/collections/cross.gif' /></div></div>").appendTo(document.body);
                
                $("div#content_zoom").draggable({ cursor: 'crosshair', axis: 'y', containment: [0, 200, 600, 600]});
                
                //imageZoom = jQuery(msg).appendTo('#content_zoom');
                functions.clickToHide();
			}			
		});   
	},
	
	hideZoom: function(my_div) {
    	background.animate({opacity: 0}, function() {
            jQuery(this).remove();
        });
        imageZoom.animate({opacity: 0}, function() {
            jQuery(this).remove();
        });
        /*closeButton.animate({opacity: 0}, "slow", function() {
            jQuery(this).remove();
        });*/
    },
    
    
    // Appel AJAX pour les favoris
	showFavorite: function(my_bloc){
	   var colid = my_bloc.parent().attr("idc");
       var famid = my_bloc.parent().attr("idf");
       var refid = my_bloc.parent().attr("idr");
       
	   
       $.ajax({
			type: 'POST',
			url: '?ajax_collections',
			data: 'action=showFavorite&colid='+colid+'&famid='+famid+'&refid='+refid,
			
			success: function(msg) {

                background = jQuery("<div id='background_items'></div>").css({opacity: 0.6}).appendTo(document.body);
                favorite = jQuery("<div id='bloc_favorite'><div id='close' class='close'><img src='pages/_img/collections/cross.gif' /></div>"+msg+"</div>").appendTo(document.body);
                //closeButton = jQuery("<div id='close' class='close'><div id='close_favorite'><img src='pages/_img/collections/cross.gif' /></div></div>").appendTo(document.body);
                
                $("div#bloc_favorite").draggable({ cursor: 'crosshair', containment: [10, 0, 650, 525]});
                
                //imageZoom = jQuery(msg).appendTo('#content_zoom');
                functions.clickToHideFavorite();
			}			
		});   
	},
	
	hideFavorite: function(my_div) {
    	background.animate({opacity: 0}, function() {
            jQuery(this).remove();
        });
        favorite.animate({opacity: 0}, function() {
            jQuery(this).remove();
        });
    },
	
	
	// Appel AJAX pour les specials
	// J'ai pas mis beaucoup de commentaires, pas bien...
	showSpecial: function(my_bloc){
	   
       var id = my_bloc.attr("id"); //Type d'action (images, texte, ...)
       var famid = my_bloc.parent().attr("idf");
       //Si on a le piccode, c'est que la référence actuellement affichée a été chargée en ajax
       if(piccode) {
            refid=piccode;
       }else {  //Sinon c'est la référence par défaut (la première quand on arrive sur la page)
            var refid = my_bloc.parent().attr("idr");
       }
       var lang = my_bloc.parent().attr("idl");

       $.ajax({
			type: 'POST',
			url: '?ajax_collections',
			data: 'action=showSpecial&dataType='+id+'&famid='+famid+'&refid='+refid+'&lang='+lang,
			
			success: function(msg) {
                
                //$("div#specials_overflow").html("<div id='items_special'></div>");
                $("div#items_special").html(msg);
                
                //Si le flag_special_items n'est pas à 1, c'est que le bloc des spécials n'est pas affiché
                if(flag_special_items==0) {
                    flag_special_items=1;
                    last_special_items = id;
                    
                    $("div#background_special").toggle("slow");
                    $("div#content_special").toggle("slow");
                    //$("div#content_special").children("div").css("display", "block");
                    //$("div#content_special").children("div").children().show();
                    //$("div#content_special").children("div").children().css("display", "block");
				    //$("div#article_overflow").show("slow");
				    
                } else if(flag_special_items==1 && id!=last_special_items) {
                    flag_special_items=1;
                    last_special_items = id;
                    $("div#content_special").show("slow");
				    //$("div#article_overflow").show("slow");
				    //onloadPrime('specials_overflow');
                } else if(flag_special_items==1 && id==last_special_items) {
                    flag_special_items=0;
                    $("div#background_special").toggle("slow");
                    $("div#content_special").toggle("slow");
                    //jQuery("<div id='content_special'></div>").css({display: "none"});
				    //$("div#article_overflow").toggle("slow");
                }
                if(nb_special==0 && id=='text') {
                    onloadPrime('specials_overflow');
                }
                $('.lightbox_special').lightBox();
                nb_special++;
                skinScroll.refresh();
			}			
		});   
	},
	
	hideSpecial: function(){
	   if(flag_special_items==1) {
            flag_special_items=0;
            $("div#background_special").toggle("slow");
		    $("div#content_special").toggle("slow");
		    //jQuery("<div id='content_special'></div>").css({display: "none"});
        }   
    }
	
};    
