$(document).ready(function(){
    $('#other_europe').hide();
    $("a[rel^='other_europe']").live('click', function(){
        if ( ! $("#main_overlay").length) {
            $('#container').prepend('<div id="main_overlay"></div>');
            $("#main_overlay").css({width:$(window).width(), height:$(window).height(), top: -$('#container').offset().top + "px", left: -$('#container').offset().left + "px"});
        }
        if ($("#main_overlay").is(':hidden')) {
            $("#main_overlay").css({opacity:'0', display:'block'});
            $("#main_overlay").animate({opacity: '0.7'}, {complete: function(){
                $("#main_overlay").live('click', function(){
                    $(this).animate({opacity: '0'}, {complete: function(){
                        $(this).remove();
                    }});
                    $('#other_europe').fadeToggle('slow');
                    return false;
                });
            }});
        } else {
            $("#main_overlay").animate({opacity: '0'}, {complete: function(){
                $(this).hide();
            }});
        }
        $('#other_europe').fadeToggle('slow');
        return false;
    });
});

jQuery.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

function displaySend(productId, country, langId) {
    $("#send_to_friend").html('<div id="loader"><img src="/media/images/mail/ajax-loader.gif" alt="" /></div>');
    if ( $("#send_to_friend").is(':hidden')) {
        if ( ! $("#main_overlay").length) {
            $("#send_to_friend").parent().prepend('<div id="main_overlay"></div>');
            $("#main_overlay").css({width:$(window).width(), height:$(window).height(), top: -$('#container').offset().top + "px", left: -$('#container').offset().left + "px"});
        }
        var viewport = getViewport();
        $("#send_to_friend").css({top:Math.round((viewport[1]-$("#send_to_friend").height())/2), left:Math.round((viewport[0]-$("#send_to_friend").width())/2)});
        if ($("#main_overlay").is(':hidden')) {
            $("#main_overlay").css({opacity:'0', display:'block'});
            $("#main_overlay").animate({opacity: '0.7'}, {complete: function(){
                $("#main_overlay, #send_to_friend #close_window a").live('click', function(){
                    $("#main_overlay").animate({opacity: '0'}, {complete: function(){
                        $("#main_overlay").remove();
                    }});
                    $('#send_to_friend').fadeOut('slow');
                    return false;
                });
            }});
            $('#send_to_friend').fadeIn('slow');
        } else {
            $("#main_overlay").animate({opacity: '0'}, {complete: function(){
                $(this).hide();
            }});
            $("#send_to_friend").fadeOut('slow');
        }
    } else {
        $("#send_to_friend").fadeOut('slow');
        return;
    }

    $.ajax({
        url: "/" + (country + '').toLowerCase() + "/send/" + langId + "/" + productId + "/",
        type: "GET",
        dataType: "html",
        timeout: 0,
        error: function(html){
            alert('erreur ajax');
        },
        success : function(html){
            $("#send_to_friend").html(html);
            handleSendToFriend(productId, country, langId);
        }
    });
}

function handleSendToFriend(productId, country, langId){
    var data = {};
    $("#send_to_friend > form:first").submit(function(){
        $("#send_to_friend > form:first :input").each(function(){
            data[$(this).attr("name")] = $(this).attr("value");
        });
        $("#send_to_friend > form:first input[class='submit']").parent().html('<img src="/media/images/mail/ajax-loader-submit.gif" alt="" />');
        $.ajax({
            url: "/" + (country + '').toLowerCase() + "/send/" + langId + "/" + productId + "/",
            type: "POST",
            dataType: "html",
            data: data,
            timeout: 0,
            error: function(html){
                alert('erreur ajax');
            },
            success : function(html){
                $("#send_to_friend").html(html);
                handleSendToFriend(productId, country, langId);
            }
        });
        return false;
    });
}

function displayCGV(country, tag){
    var country = country == null ? 'en' : country.toLowerCase();
    if ( ! $("#cgv").length) {
        $('body').append('<div id="cgv"></div>');
    }
    if ( $("#cgv").is(':hidden')) {
        if ( ! $("#main_overlay").length) {
            $("#cgv").parent().prepend('<div id="main_overlay"></div>');
            $("#main_overlay").css({width:$(window).width(), height:$(window).height(), top: -$('#container').offset().top + "px", left: -$('#container').offset().left + "px"});
        }
        var viewport = getViewport();
        $("#cgv").css({top:Math.round((viewport[1]-$("#cgv").height())/2), left:Math.round((viewport[0]-$("#cgv").width())/2), height:'auto'});
        if ($("#main_overlay").is(':hidden')) {
            $("#main_overlay").css({opacity:'0', display:'block'});
            $("#main_overlay").animate({opacity: '0.7'}, {complete: function(){
                $("#main_overlay").click(function(){
                    $(this).animate({opacity: '0'}, {complete: function(){
                        $(this).remove();
                    }});
                    $('#cgv').fadeOut('slow', function(){
                        $(this).remove();
                    });
                });
            }});
            $('#cgv').fadeIn('slow');
        } else {
            $("#main_overlay").animate({opacity: '0'}, {complete: function(){
                $(this).remove();
            }});
            $("#cgv").fadeOut('slow', function(){
                $(this).remove();
            });
        }
    } else {
        $("#cgv").fadeOut('slow');
        return;
    }

    updateCGV(country, 0, '', tag);

}

function updateCGV(country, article, type, tag) {
    var article = article != null ? article : 0;
    if (type != 'full') {
        var elementToUpdate = $("#cgv_article").length ? $("#cgv_article") : $("#cgv");
        var isPartialUpdate = $("#cgv_article").length ? 1 : 0;
        if (isPartialUpdate) {
            elementToUpdate.fadeToggle('slow', function(){
                $.ajax({
                    url: "/" + (country + '').toLowerCase() + "/cgv/"  + article + "/" + isPartialUpdate + "/",
                    type: "GET",
                    dataType: "html",
                    timeout: 0,
                    error: function(html){
                        alert('erreur ajax');
                    },
                    success : function(html){
                        elementToUpdate.html(html);
                        if (isPartialUpdate) {
                            elementToUpdate.fadeToggle('slow');
                        }
                        $('#cgv_article_content').jScrollPane({scrollbarWidth:7, showArrows: true, animateTo: true});
                    }
                });
            });
        } else {
            $.ajax({
                url: "/" + (country + '').toLowerCase() + "/cgv/"  + article + "/" + isPartialUpdate + "/",
                type: "GET",
                dataType: "html",
                timeout: 0,
                error: function(xhr,err,e){
                    alert('Error : ajax');
                },
                success : function(html){
                    elementToUpdate.html(html);
                    $('#cgv_article_content').jScrollPane({scrollbarWidth:7, showArrows: true, animateTo: true});
                    $("#cgv_menu ul li a").click(function(){
                        var anchor = $(this).attr('href');
                        anchor = anchor.substr(anchor.lastIndexOf('#')); // IE Fix
                        $('#cgv_article_content')[0].scrollTo($('#cgv_article_content ' + anchor).offset().top - $('#cgv_article_content').offset().top);
                        return false;
                    });
                    $("#cgv_close a").click(function(){
                        $("#main_overlay").animate({opacity: '0'}, {complete: function(){
                            $(this).remove();
                        }});
                        $("#cgv").fadeOut('slow', function(){
                            $(this).remove();
                        });
                        return false;
                    });
                    $('.print a').click(function(){
                        var content = '<h2>' + $('#cgv_main h2').html() + '</h2>' + $('#cgv_article_content').html();
                        $('body').append('<div id="tmp_div">' + content + '</div>');
                        $('#tmp_div').jqprint({ operaSupport: true, printContainer:false});
                        $('#tmp_div').remove();
                        return false;
                    });
                    $('#cgv_article_content')[0].scrollTo($("#cgv_" + (tag == null ? article : tag) + "").offset().top - $('#cgv_article_content').offset().top);
                }
            });
        }
    } else {
        $('#cgv_article_content')[0].scrollTo($("#cgv_" + (tag == null ? article : tag) + "").offset().top - $('#cgv_article_content').offset().top);
    }
}

function getPageScroll(){
		var xScroll, yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}

		return [xScroll, yScroll]; 
}

function getViewport(){
		var scroll = getPageScroll();

		return [$(window).width(), $(window).height(), scroll[0], scroll[1]];
}

function displayPDV(country){


	
    var country = country != null ? country : 'fr';
		
	if ( ! $("#pdv").length) {
        $('body').append('<div id="pdv"></div>');
    }

    if ( $("#pdv").is(':hidden')) {
        if ( ! $("#main_overlay").length) {
            $("#pdv").parent().prepend('<div id="main_overlay"></div>');
            $("#main_overlay").css({width:$(window).width(), height:$(window).height(), top: -$('#container').offset().top + "px", left: -$('#container').offset().left + "px"});
        }

        var viewport = getViewport();
        $("#pdv").css({top:Math.round((viewport[1]-$("#pdv").height())/2), left:Math.round((viewport[0]-$("#pdv").width())/2)});
        if ($("#main_overlay").is(':hidden')) {
            $("#main_overlay").css({opacity:'0', display:'block'});
            $("#main_overlay").animate({opacity: '0.7'}, {complete: function(){
                $("#main_overlay").click(function(){
                    $(this).animate({opacity: '0'}, {complete: function(){
                        $(this).remove();
                    }});
                    $('#pdv').fadeOut('slow', function(){
                        $(this).remove();
                    });
                });
            }});
            $('#pdv').fadeIn('slow');
        } else {
            $("#main_overlay").animate({opacity: '0'}, {complete: function(){
                $(this).remove();
            }});
            $("#pdv").fadeOut('slow', function(){
                $(this).remove();
           });
        }
    } else {
       $("#pdv").fadeOut('slow');
        return;
   }

    updatePDV(country);
}

function updatePDV(country) {

	var elementToUpdate = $("#pdv");
	
	$.ajax({
		url: "/" + (country + '').toLowerCase() + "/pdv/",
		type: "GET",
		dataType: "html",
		timeout: 0,
		error: function(xhr,err,e){
			alert('Error : ajax');
		},
		success : function(html){
			elementToUpdate.html(html);
			$("#pdv_close a").click(function(){
				$("#main_overlay").animate({opacity: '0'}, {complete: function(){
					$(this).remove();
				}});
				$("#pdv").fadeOut('slow', function(){
					$(this).remove();
				});
				return false;
			});
			$("#pdv_content .close_it").click(function(){
				$("#main_overlay").animate({opacity: '0'}, {complete: function(){
					$(this).remove();
				}});
				$("#pdv").fadeOut('slow', function(){
					$(this).remove();
				});
				return false;
			});			
		}
	});
	
}

function displayPayment(pLang, pcountry){
	
    var country = country != null ? country : 'fr';
		
	if ( ! $("#payment").length) {
        $('body').append('<div id="payment"></div>');
    }

    if ( $("#payment").is(':hidden')) {
        if ( ! $("#main_overlay").length) {
            $("#payment").parent().prepend('<div id="main_overlay"></div>');
            $("#main_overlay").css({width:$(window).width(), height:$(window).height(), top: -$('#container').offset().top + "px", left: -$('#container').offset().left + "px"});
        }

        var viewport = getViewport();
        $("#payment").css({top:Math.round((viewport[1]-$("#payment").height())/2), left:Math.round((viewport[0]-$("#payment").width())/2)});
        if ($("#main_overlay").is(':hidden')) {
            $("#main_overlay").css({opacity:'0', display:'block'});
            $("#main_overlay").animate({opacity: '0.7'}, {complete: function(){
                $("#main_overlay").click(function(){
                    $(this).animate({opacity: '0'}, {complete: function(){
                        $(this).remove();
                    }});
                    $('#payment').fadeOut('slow', function(){
                        $(this).remove();
                    });
                });
            }});
            $('#payment').fadeIn('slow');
        } else {
            $("#main_overlay").animate({opacity: '0'}, {complete: function(){
                $(this).remove();
            }});
            $("#payment").fadeOut('slow', function(){
                $(this).remove();
           });
        }
    } else {
       $("#payment").fadeOut('slow');
        return;
   }
   
	updatePayment(pLang, pcountry);
}

function updatePayment(pLang, pcountry) {

	var elementToUpdate = $("#payment");
	
	$.ajax({
		url: "/_remoting/mods/payment/call_request.php?lng="+pLang+"&cnt="+pcountry,
		type: "GET",
		dataType: "html",
		timeout: 0,
		error: function(xhr,err,e){
			alert('Error : ajax');
		},
		success : function(html){
			elementToUpdate.html(html);		
		}
	});
	
}