//config vars

var application_url    = 'http://www.coolfancydress.com/geo-popup';

var gpp_lightbox_fadein_time = 500;
var gpp_accept_country_list = ["AT", "BE", "BG", "CH", "CZ", "DE", "DK", "EE", "ES", "FI", "FR", "GB", "GR", "HU", "HR", "IE", "IT", "LU", "NO", "NL", "PO", "PT", "SE"];







/*
GEOIP_COUNTRY_CODES
"AP", "EU", "AD", "AE", "AF", "AG", "AI", "AL", "AM", "AN", "AO", "AQ", "AR", "AS", "AT", "AU", "AW", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH",
"BI", "BJ", "BM", "BN", "BO", "BR", "BS", "BT", "BV", "BW", "BY", "BZ", "CA", "CC", "CD", "CF", "CG", "CH", "CI", "CK", "CL", "CM", "CN", "CO", "CR", "CU",
"CV", "CX", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "EH", "ER", "ES", "ET", "FI", "FJ", "FK", "FM", "FO", "FR", "FX", "GA", "GB",
"GD", "GE", "GF", "GH", "GI", "GL", "GM", "GN", "GP", "GQ", "GR", "GS", "GT", "GU", "GW", "GY", "HK", "HM", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IN",
"IO", "IQ", "IR", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KP", "KR", "KW", "KY", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS",
"LT", "LU", "LV", "LY", "MA", "MC", "MD", "MG", "MH", "MK", "ML", "MM", "MN", "MO", "MP", "MQ", "MR", "MS", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA",
"NC", "NE", "NF", "NG", "NI", "NL", "NO", "NP", "NR", "NU", "NZ", "OM", "PA", "PE", "PF", "PG", "PH", "PK", "PL", "PM", "PN", "PR", "PS", "PT", "PW", "PY",
"QA", "RE", "RO", "RU", "RW", "SA", "SB", "SC", "SD", "SE", "SG", "SH", "SI", "SJ", "SK", "SL", "SM", "SN", "SO", "SR", "ST", "SV", "SY", "SZ", "TC", "TD",
"TF", "TG", "TH", "TJ", "TK", "TM", "TN", "TO", "TL", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "UM", "US", "UY", "UZ", "VA", "VC", "VE", "VG", "VI", "VN",
"VU", "WF", "WS", "YE", "YT", "RS", "ZA", "ZM", "ME", "ZW", "A1", "A2", "O1", "AX", "GG", "IM", "JE", "BL", "MF"
*/

//
//
//NO NEEED TO EDIT UNDER THIS LINE!
//
//

var gpp_displayed = 0;


//0 means disabled; 1 means enabled;
var CustomLightboxStatus = 0;

//CONTROLLING EVENTS IN jQuery
jQuery(document).ready(function(){
    
    loadScript('http://j.maxmind.com/app/geoip.js', function() {

        var gpp_displayed = getCookie('cpopup_displayed');
        if (gpp_displayed < 0 || gpp_displayed == "")
            {
                //check if in the list
                if (gpp_accept_country_list.in_array(geoip_country_code()) === false)
                    {
                        //run the popup
                        cookiecheck();
                    }            
            }

    });
    
    jQuery("#CustomLightboxPopup").find('#pp_close').live('click', function(){
        var CustomLightboxObject = new CustomLightbox();
        CustomLightboxObject.Hide();
        return false;
        
    });
    //Click out event!
    jQuery("#backgroundCustomLightbox").live('click', function(){
        var CustomLightboxObject = new CustomLightbox();
        CustomLightboxObject.Hide();
        return false;
    });
    //Press Escape event!
    jQuery(document).keypress(function(e){
        if(e.keyCode==27 && CustomLightboxStatus==1)
            {
                var CustomLightboxObject = new CustomLightbox();
                CustomLightboxObject.Hide();
                return false;
            }
    });

});


function loadScript(url, callback)
    {

        var script = document.createElement("script")
        script.type = "text/javascript";

        if (script.readyState){  //IE
            script.onreadystatechange = function(){
                if (script.readyState == "loaded" ||
                        script.readyState == "complete"){
                    script.onreadystatechange = null;
                    callback();
                }
            };
        } else {  //Others
            script.onload = function(){
                callback();
            };
        }

        script.src = url;
        document.getElementsByTagName("head")[0].appendChild(script);
    }
    
Array.prototype.in_array = function(p_val) 
    {
        for(var i = 0, l = this.length; i < l; i++) {
            if(this[i] == p_val) {
                return true;
            }
        }
        return false;
    }


function cookiecheck()
    {
        setCookie('cpopup_displayed',1,1)
        if (CustomLightboxStatus == 0)
            CustomLightboxLoad();
    }
    
    
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());
    }
    

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 CustomLightboxLoad()
    {
        //load the content
        jQuery.ajax({
                type: "POST",
                url: application_url + '/content/data.html',
                success: function(html)
                    {
                        var CustomLightboxObject = new CustomLightbox();
                        CustomLightboxObject.Init(720);
                        
                        html = update_content_variables(html);
                        
                        CustomLightboxObject.SetContent(html);
                        CustomLightboxObject.Show();
                    }
            });
    
    }
    
function CustomLightboxClose()
    {
        var CustomLightboxObject = new CustomLightbox();
        CustomLightboxObject.Hide();
        return false;
    }


//update the content variables
function update_content_variables(html)
    {
        html = html.replace(/%client_country%/g, geoip_country_name());
        html = html.replace(/%application_url%/g, application_url);
        
        return (html);
    }


function CustomLightbox()
    {

    }

CustomLightbox.prototype.Init = function( x_dim, y_dim )
    {
        if (CustomLightboxStatus == 1)
            return false;
        this.appendMask();
        
        this.appendPopupBody(x_dim, y_dim);
        
    };
    
CustomLightbox.prototype.appendMask = function( recache )
    {
        //check if there's a Mask in palce
        if (jQuery('#backgroundCustomLightbox').length == 0)
            {
                jQuery('body').append('<div id="backgroundCustomLightbox"></div>');
            }
            
        //update the mask dimensions
        var windowWidth = jQuery(document).width();
        var windowHeight = jQuery(document).height();

        jQuery('#backgroundCustomLightbox').height(windowHeight);
        
    };
    
CustomLightbox.prototype.appendPopupBody = function( x_dim, y_dim )
    {   
        var PopupBodyHTML = '<div id="CustomLightboxPopup" class="pp_pic_holder facebook">'
                            +' <div class="pp_top"><div class="pp_left"></div>        <div class="pp_middle"></div>        <div class="pp_right"></div>       </div>'
                            +' <div class="pp_content_container">        <div class="pp_content">      </div>       '
                            +' <div class="pp_details clearfix"><p class="pp_status"></p>            <a href="#" id="pp_close">Close</a></div>'
                            +' </div>      '
                            + '<div class="pp_bottom">        <div class="pp_left"></div>        <div class="pp_middle"></div>        <div class="pp_right"></div>       </div>      </div>';
                            
        //check if there's a Mask in palce
        if (jQuery('#CustomLightboxPopup').length == 0)
            {
                jQuery('body').append(PopupBodyHTML);
            }
            
        jQuery('#CustomLightboxPopup').width(x_dim);
        
        if (y_dim > 0)
            jQuery('#CustomLightboxPopup .pp_content').height(y_dim);    
    };
    
CustomLightbox.prototype.SetContent = function( HTMLContent )
    {
        jQuery('#CustomLightboxPopup .pp_content').html(HTMLContent);
    };
    
CustomLightbox.prototype.SetStatus = function( TXTstatus )
    {
        var status = jQuery('#CustomLightboxPopup .pp_details .pp_status');
        jQuery(status).fadeTo(0, 0); 
        jQuery(status).html(TXTstatus);
        jQuery(status).css('display','block');
        jQuery(status).fadeTo(400, 1, function() {
            jQuery(status).animate({
                    opacity: 1
                  }, 2000, function (){
                   jQuery(status).fadeTo(400, 0);
                  })                     
            
        }); 
    };
    
CustomLightbox.prototype.Show = function( recache )
    {
         //center the pop-up
        this.centerPopup();
        
        jQuery('#backgroundCustomLightbox').fadeTo(0, 0);
        jQuery('#backgroundCustomLightbox').css('display','block');
        jQuery('#backgroundCustomLightbox').fadeTo(gpp_lightbox_fadein_time, 0.6);
        
        jQuery('#CustomLightboxPopup').fadeTo(0, 0);
        jQuery('#CustomLightboxPopup').css('display','block');
        jQuery('#CustomLightboxPopup').fadeTo(gpp_lightbox_fadein_time, 1);
        
        CustomLightboxStatus = 1;
    };

CustomLightbox.prototype.Hide = function( recache )
    {

        jQuery('#CustomLightboxPopup').fadeOut(gpp_lightbox_fadein_time);        
        jQuery('#backgroundCustomLightbox').fadeOut(gpp_lightbox_fadein_time);
        
        CustomLightboxStatus = 0;
    };            

         
//centering popup
CustomLightbox.prototype.centerPopup = function( recache )
    {
        
        //request data for centering
        var windowWidth = jQuery(window).width();
        var windowHeight = jQuery(window).height();
        var popupHeight = jQuery("#CustomLightboxPopup").height();
        var popupWidth = jQuery("#CustomLightboxPopup").width();
        
        //centering
        jQuery("#CustomLightboxPopup").css({
            "position": "absolute",
            "top": windowHeight/2-popupHeight/2,
            "left": windowWidth/2-popupWidth/2
        });
        
    };
