// JavaScript Document  

(function($){
    var _slideDown = jQuery.fn.slideDown;
    
    $.fn.slideDown = function()
    {
        var oldCallback = arguments[1];
        
        _slideDown.call( this, arguments[0], function(){
            if(oldCallback != undefined)
            {
                oldCallback();
            }
                $(document).trigger('equalize');
        } );
        
        return $(this);
    }
    
    var _slideUp = jQuery.fn.slideUp;
    
    $.fn.slideUp = function()
    {
        var oldCallback = arguments[1];
        
        _slideUp.call( this, arguments[0], function(){
            if(oldCallback != undefined)
            {
                oldCallback();
            }
                $(document).trigger('equalize');
        } );
        
        return $(this);
    }
    
    $.fn.imageButton = function()
    {
        var img = arguments[0];
        $(this).html('<img src="'+img+'" />');
        $(this).addClass('button');
        
        return $(this);
    }
    
    $.fn.validatePhone = function()
    {
        var field = $(this);
        var expReg = /^(\d{3}-?\d{3}-?\d{4,6})$/;
        
        if(expReg.test(field.val()) == false || field.val() == "")
        {
            arguments[0].call(this);
            return false;
        }
            
        
        return true;
    }
    
    
})(jQuery);


jQuery(window).load(function(){
    $(document).trigger( 'equalize' );
});

jQuery(document).ready(function()
{
	var lang = ( function() {
		var flag ;
		$.ajax({
			async: false,
			type: "POST",
			url: "/ajax_proxy/",
			data: "fct=getLang",
			dataType: 'json',
			success: function(data) {flag = data.lang;}
		});
		return(flag) ;
	} )() ;
	
//    if(jQuery.browser.version == "6.0" && $.browser.msie)
    
    $('#main_content').addClass("column");
    
    // Bind a custom 'equalize' event to document, then trigger it immediately.
    $(document).bind( 'equalize', equalize ).trigger( 'equalize' );
    
    // check cookies
    if(navigator.cookieEnabled === false)
    {
        
        
    }
    
    // This one function perform multiple equalizeBottoms calls.
    function equalize()
    {
        $('#left_menu_and_main_content').each(function()
        {
            $(this).find('.column > :last-child').equalizeBottoms();
        });
    
        if($.browser.msie && $.browser.version == '6.0')
        {
            var hh = $('#left_menu').height();
            $('#left_menu').css("height", hh+"px");
        }
    };
    
    $("#left_menu_promobox ul li:last").addClass("last-item");

    $('#register_to_newsletter_mail').keydown(function(event){
    	if (event.keyCode == '13') validate_register_to_newsletter(lang);
    });

    $("#fanbox").click(function() {
        
        var currentURL         = window.location.pathname;
        var a                  = currentURL.split('/');
		if(a[2] == 'studio') a[2] = 'student';
        
        a[2] = a[2] == 'student' || a[2] == 'teacher' ? a[2] : 'student';
        a[1] = a[1] == 'fr' || a[1] == 'en' ? a[1] : 'fr';
        
        document.location.href = "/" + a[1] + "/" + a[2] + "/signup/"; 
    });
    
});

function create_lightbox(application)
{
	$("#wrap").append("<div id=\"lightbox\"></div>");

	$("#lightbox").css({"width"    : "100%",
                        "height"   : "100%",
                        "position" : "absolute",
                        "left"     : "0px",
                        "top"      : "0px",
                        "background-color" : "#999999",
                        "position" : "fixed",
                        "z-index"  : 500,
                        "opacity"  : "0"}).fadeTo("slow", 0.8);
    
    $("#lightbox").click(function() {
        close_lightbox();
        close_application(application);
    });
}

function close_lightbox()
{
	$("#lightbox").fadeTo("fast", 0, function()	{
    	$("#lightbox").remove();
    });
}

function close_application(application)
{
	$("#" + application).fadeOut("fast", function() {
    	close_lightbox();
        
        if($(this).find(".todelete").length > 0)
            $(this).remove();
    });
}

function open_inscription(class_id, teacher_id, day, month, application, content, callback, dataUsed)
{
	if ($("#" + application).is(":hidden")) {
        $("#error").html('');
        $("#register").css("display", "none");
        $("#class_over").css("display", "none");
        $("#"+content).css("display", "block");
    }
    
    open_application(application);

    // THE SPAN ARE IN THE class-inscription.php FILE
    $("#class_id").val(class_id);
    $("#teacher_id").val(teacher_id);
    $("#from_url").val($.address.value());
    $("#subscribe_button").attr("href", $("#subscribe_button").attr("href") + "?class_id=" + class_id + "&teacher_id=" + teacher_id);
		
	if(callback) {
		$("#data_subscribe").val(dataUsed) ;
	}
}

function open_application(application)
{
    if($("#" + application).parent() != $("body"))
        $("#" + application).appendTo($("body"));
    
    if ($("#" + application).is(":hidden")) {
        create_lightbox(application);

        var width_application  = $("#" + application).width() / 2;
        var height_application = $("#" + application).height() / 2;

        $("#" + application).css({"position"    : "fixed",
                                 "left"         : "50%",
                                 "margin-left"  : "-" + width_application + "px",
                                 "top"          : "50%",
                                 "margin-top"   : "-" + height_application + "px",
                                 "z-index"      : "5000"});

        $("#" + application).fadeIn("slow");
    }
}

function show_style_description(styleId)
{
    $.post("/ajax_proxy/", {fct: "get_style_description", id: styleId}, function(data) {
        
        if(data != null && data["definition"] != '') {
        
            $("#style_title").html(data["description"]);
            $("#style_description").html(data["definition"]);
        
            open_dialog("style_dialog");
        }
        
    }, "json")
}

function create_custom_application(container_name, content, title)
{
    var application   = "<div id=\"" + container_name + "\" class=\"yt-dialog\">"+
                            "<input type=\"hidden\" class=\"todelete\" />"+
                            "<div class=\"yt-dialog-top\"></div>"+
                            "<div class=\"yt-dialog-middle\">"+
                                "<div class=\"yt-dialog-middle-top\">"+
                                    "<div class=\"dialog-logo\"><img src=\"/medias/images/logo/yogatribes-small-2.png\" alt=\"\" /></div>"+
                                    "<div class=\"dialog-title\">"+title+"</div>"+
                                    "<div class=\"dialog-close\"><a href=\"javascript:void(0)\" onclick=\"close_application('"+container_name+"')\"><img src=\"/medias/images/common/close.png\" width=\"12\" height=\"12\" alt=\"\" /></a></div>"+
                                    "<div class=\"clear\"></div>"+
                                "</div>"+
                                "<div class=\"separator_grey\" style=\"margin:0 6px\"></div>"+ 
                                "<div class=\"yt-dialog-middle-content\">"+content+"</div>"+
                            "</div>"+
                            "<div class=\"yt-dialog-bottom\"></div>"+
                            "<div class=\"clear\"></div>"+
                         "</div>";
    
    $("#" + container_name).css({"visibility":"hidden", "display":"block"});
    create_lightbox(container_name);
    $("body").append(application);
    
    var width_application   = $("#" + container_name).width() / 2;
    var height_application  = $("#" + container_name).height() / 2;
    
    $("#" + container_name).css({"visibility":"", "display":"none"});
    $("#" + container_name).css({
                              "position": "fixed",
                              "left": "50%",
                              "margin-left": "-" + width_application + "px",
                              "top": "50%",
                              "margin-top": "-" + height_application + "px",
                              "z-index": "5000"}).fadeIn("slow");     
    
    $("#"+container_name+" .yt-dialog-middle-content").css({padding:"10px 20px"});
}

function open_pop_up(path, name, width, height)
{
    var center_width  = (window.screen.width - width) / 2;
    var center_height = (window.screen.height - height) / 2;

	window.open(path, name, 'width=' + width + ', height=' + height + ', location=no, left=' + center_width + ', top=' + center_height + ', scrollbars = yes');
}


function get_month(current, lang)
{
    if(current < 1) current = 12;
    else if(current > 12) current = 1;

    var monthList = lang == 2 ? {
        '1': 'janvier', 
        '2': 'f\351vrier', 
        '3': 'mars', 
        '4': 'avril', 
        '5': 'mai', 
        '6': 'juin', 
        '7': 'juillet', 
        '8': 'ao\373t', 
        '9': 'septembre', 
        '10': 'octobre', 
        '11': 'novembre', 
        '12': 'd\351cembre'
    } : {
        '1': 'January', 
        '2': 'February', 
        '3': 'March', 
        '4': 'April', 
        '5': 'May', 
        '6': 'June', 
        '7': 'July', 
        '8': 'August', 
        '9': 'September', 
        '10': 'October', 
        '11': 'November', 
        '12': 'December'   
    };
    
    return monthList[String(Number(current))];
}

function display_message(message_div_id, text, success)
{
    $("#" + message_div_id).html(text).css("display", "");
    
    if (success == true)
    {
        $("#" + message_div_id).removeClass('error_message').addClass('valid_message').delay(5000).slideUp("slow");
    }
    else
    {
        $("#" + message_div_id).removeClass('valid_message').addClass('error_message').delay(10000).slideUp("slow");
    }
}

function validate_register_to_newsletter(lang)
{
    if ($("#register_to_newsletter_mail").val() == "")
    {
        if (lang == 2)
        {
            display_message("error_message_register_to_newsletter", "Veuillez entrer votre courriel.", false);
        }
        else
        {
            display_message("error_message_register_to_newsletter", "Please enter your email.", false);
        }

        return false;
    }

    if (validMail($("#register_to_newsletter_mail").val()) == false)
    {
        if (lang == 2)
        {
            display_message("error_message_register_to_newsletter", "Courriel invalide.", false);
        }
        else
        {
            display_message("error_message_register_to_newsletter", "Invalid email.", false);
        }

        return false;
    }
    
    register_to_newsletter(lang);
    return false;
}

function register_to_newsletter(lang)
{
    $.post("/api/service.php", {service: "register_to_newsletter", email:$("#register_to_newsletter_mail").val()},
    function(data)
    {
        if (data == true)
        {
            if (lang == 2)
            {
                display_message("error_message_register_to_newsletter", "Merci, vous &ecirc;tes maintenant enregistr&eacute;.", true);
                clear_input_text("register_to_newsletter_mail");
            }
            else
            {
                display_message("error_message_register_to_newsletter", "Thank you, you are now registered.", true);
                clear_input_text("register_to_newsletter_mail");
            }
        }
        else
        {
            if (lang == 2)
            {
                display_message("error_message_register_to_newsletter", "Une erreur inconnue est survenue!", false);
            }
            else
            {
                display_message("error_message_register_to_newsletter", "An unknown error occured!", false);
            }
        }
    });
}

function open_upload_dialog(application)
{
	if ($("#" + application).is(":hidden"))
	{
		create_lightbox(application);

		var width_application  = $("#" + application).width() / 2;
		var height_application = $("#" + application).height() / 2;

		$("#" + application).css({"position": "fixed",
                                 "left": "50%",
                                 "margin-left": "-" + width_application + "px",
                                 "top": "50%",
                                 "margin-top": "-" + height_application + "px",
                                 "z-index": "5000"});

		$("#" + application).fadeIn("slow", function(){
            $("#" + application).trigger("windowOpen");
            
        });
	}
}

function open_dialog(application)
{
	if ($("#" + application).is(":hidden"))
	{
		create_lightbox(application);

		var width_application  = $("#" + application).width() / 2;
		var height_application = $("#" + application).height() / 2;

		$("#" + application).css({"position": "fixed",
                                 "left": "50%",
                                 "margin-left": "-" + width_application + "px",
                                 "top": "50%",
                                 "margin-top": "-" + height_application + "px",
                                 "z-index": "5000"});

		$("#" + application).fadeIn("slow");
	}
};

function validMail(mail)
{
    var regEmail  = /^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/;
    
    return regEmail.test(mail);
};


function clear_input_text(input_text)
{
    $("#" + input_text).val("");
};

