/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!     
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var buttonId;

//loading popup with jQuery magic!
function loadPopup(){
    //loads popup only if it is disabled
    if(buttonId==0){
        if(popupStatus==0){
            $("#backgroundPopup").css({
                "opacity": "0.7"
            });
            $("#backgroundPopup").fadeIn("slow");
            $("#loginFormPopup").fadeIn("slow");
            popupStatus = 1;
        }
    } 
    else if(buttonId==1) {
        if(popupStatus==0){
            $("#backgroundPopup").css({
                "opacity": "0.7"
            });
            $("#backgroundPopup").fadeIn("slow");
            $("#signUpFormPopup").fadeIn("slow");
            popupStatus = 1;
        }
    }
	else if(buttonId==2) {
        if(popupStatus==0){
            $("#backgroundPopup").css({
                "opacity": "0.7"
            });
            $("#backgroundPopup").fadeIn("slow");
            $("#planToWatchForm").fadeIn("slow");
            popupStatus = 1;
        }
    }
	else if(buttonId==3) {
        if(popupStatus==0){
            $("#backgroundPopup").css({
                "opacity": "0.7"
            });
            $("#backgroundPopup").fadeIn("slow");
            $("#contactUsForm").fadeIn("slow");
            popupStatus = 1;
        }
    }
}

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    if(buttonId==0){
        if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#loginFormPopup").fadeOut("slow" , function() {
                $(this).children().remove();
            });
            popupStatus = 0;
        }
    } else if(buttonId==1) {
        if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#signUpFormPopup").fadeOut("slow", function() {
                $(this).children().remove();
            });
            popupStatus = 0;
        }
    } else if(buttonId==2) {
        if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#planToWatchForm").fadeOut("slow");
            popupStatus = 0;
        }
    }else if(buttonId==3) {
        if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#contactUsForm").fadeOut("slow");
            popupStatus = 0;
        }
    }
}

//centering popup
function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#loginFormPopup").height();
    var popupWidth = $("#loginFormPopup").width();
    //centering
    myWidth = document.documentElement.clientWidth; 
    myHeight = document.documentElement.clientHeight; 
    if(buttonId==0){
        $("#loginFormPopup").css({
            "position": "fixed",
            "top": windowHeight/2-151,
            "left": windowWidth/2-150
        });
    }
    else if (buttonId==1){
        $("#signUpFormPopup").css({
            "position": "fixed",
            "top": windowHeight/2-173,
            "left": windowWidth/2-150
        });
    }
	else if (buttonId==2){
        $("#planToWatchForm").css({
            "position": "fixed",
            "top": windowHeight/2-105,
            "left": windowWidth/2-146
        });
		
    }
	else if (buttonId==3){
        $("#contactUsForm").css({
            "position": "fixed",
            "top": windowHeight/4-140,
            "left": windowWidth/2-150
        });
		
    }
    //only need force for IE6
 
    $("#backgroundPopup").css({
        "height": windowHeight
    });
 
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
 
    //LOADING POPUP
    //Click the button event!
    $(".loginPop").click(function(){
        var data = 'uri='+window.location;
        $.get( loginUrl,
            function(html) {
                $('#loginFormPopup').html(html);
                buttonId=0;
                //centering with css
                centerPopup();
                //load popup
                loadPopup();
                $.get( facebookUrl ,
                    data,
                    function(html){
                        $('#facebookBtn').html(html);
                    }
                )
            });
        return false;
  
    });
    $(".signupPop").click(function(){
        
        $.get( signUpUrl,
            function(html) {
                $('#signUpFormPopup').html(html);
                buttonId=1;
                //centering with css
                centerPopup();
                //load popup
                loadPopup();
                 $.get( facebookUrl ,
                    function(html){
                        $('#facebookBtn').html(html);
                    }
                )
            });
        return false;
        
    }); 
	 $(".WatchForm").click(function(){     
		        buttonId=2;
                //centering with css
                centerPopup();
                //load popup
                loadPopup();  
    }); 
	 $(".contactUs").click(function(){
             $.get( contactUrl,
            function(html) {
                $('#contactUsForm').html(html);
                buttonId=3;
                //centering with css
                centerPopup();
                //load popup
                loadPopup();
            });
        return false;
		 
    }); 
    //CLOSING POPUP
    //Click the x event!
    $(".popupContactClose").live('click',function(){
        disablePopup();
    });
    //Click out event!
    $("#backgroundPopup").click(function(){
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
            disablePopup();
        }
    });

});
