$(document).ready(function(){

$("#autostart").fancybox({
				'titlePosition'		: 'inside',
				'transitionIn'		: 'none',
				'transitionOut'		: 'none',
                                'centerOnScroll'        : true
			});
checkCookieAndShowPopup();
    function showPopup() {
        centerPopup('#popupWindow');
        loadPopup('#popupWindow');
    }

    function setCookie(c_name,value,expiredays)
    {
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString()) + "; path=/";
    }

    function getCookie(c_name)
    {
        if (document.cookie.length>0)
        {
            c_start=document.cookie.indexOf(c_name + "=");
            if (c_start!=-1)
            {
                c_start=c_start + c_name.length+1;
                c_end=document.cookie.indexOf(";",c_start);
                if (c_end==-1) c_end=document.cookie.length;
                return unescape(document.cookie.substring(c_start,c_end));
            }
        }
        return "";
    }

    function checkCookieAndShowPopup()
    {
        popup_times=getCookie('popup_times');
        if (popup_times!=null && popup_times!="")
        {
            setTimeout(function() {
                $("#autostart").trigger('click');
            }, 15000);
        }
        else
        {
            setTimeout(function() {
                $("#autostart").trigger('click');
            }, 5000);

        }
        update_popup_times();
    }

    function update_popup_times() {
        popup_times=getCookie('popup_times');
        if (popup_times!=null && popup_times!="")
        {
            setCookie('popup_times',parseFloat(popup_times)+1,365);
        }
        else
        {
            setCookie('popup_times',1,365);

        }
    }


    $('#EmailAlertForm').click(function(){

        var email = $('#email').val();
        //var name = $('#name').val();
        if(validateEmail(email)){
            //disablePopup('#popupWindow');
            $.fancybox.close();
            var city = $('#city').val();
            var mobile_no = $('#mobile_no').val();

            $.post("/pastdeals/addForEmailAlerts/4", {
                email: email,
                city: city,
                mobile_no:mobile_no
            });
            setCookie('delv_email',email,365);
            setCookie('delv_city',city,365);
        }

    });

    function validateEmail(email){
        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        return emailPattern.test(email);
    }

    $('#popupWindowClose').click(function(){
        disablePopup('#popupWindow');
        setCookie('delv_popup','DND',90);
    });

    $('#popupContactClose').click(function(){
        disablePopup('#popupWindow');
        popup_times=getCookie('popup_times');
        if (popup_times!=null && popup_times!="")
        {
            setCookie('popup_times',parseFloat(popup_times)+1,365);
        }
        else
        {
            setCookie('popup_times',1,365);

        }
    });

    var popupStatus = 0;
    function loadPopup(id){
        //loads popup only if it is disabled
        if(popupStatus==0){
            $("#backgroundPopup").css({
                "opacity": "0.7"
            });
            if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
            }
            $("#backgroundPopup").fadeIn("slow");
            $(id).fadeIn("fast");

            popupStatus = 1;
        }
    }
    function disablePopup(id){
        //disables popup only if it is enabled
        if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $(id).fadeOut("fast");
            if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
            popupStatus = 0;
        }
    }
    function centerPopup(id){
        //request data for centering
        var windowWidth = document.documentElement.clientWidth;
        var windowHeight = document.documentElement.clientHeight;
        var popupHeight = $(id).height();
        var popupWidth = $(id).width();
        //centering
        $(id).css({
            "position": "absolute",
            "top": windowHeight/2-popupHeight/2,
            "left": windowWidth/2-popupWidth/2
        });
    }

});
