﻿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 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 checkCookie()
        {
            currentdate = Date.parse(Date());
            lastsplash = getCookie('lastsplash');
            var lastsplash_date = 0;
            
            if (getCookie('lastsplash_date') != null && getCookie('lastsplash_date') != "")
                {
                    lastsplash_date = getCookie('lastsplash_date');
                    lastsplash_date = parseInt(lastsplash_date) + 86400000;
                }
             else
                {
                    //This occurs if there is no last splash date (i.e. First run), just sets it to
                    //1999 to ensure it isn't hit by the 24 hour limit below.
                    lastsplash_date = Date.parse("Jan 1, 1999");
                }
 
            //Check they haven't been shown one in the past 24 hours and that the advertising campaign end date isn't hit
            if ( lastsplash_date < currentdate && currentdate < Date.parse("Jan 1 2010 00:00:01") )
                {
                    //If there is no lastsplash - Next is 1. Otherwise, if it is a number that isn't 4
                    //Then increment. Otherwise, set to "stop".
                    if (lastsplash == null || lastsplash == "") { nextsplash = "1"; } 
                    else if (lastsplash != "stop" && lastsplash != "4") { nextsplash = Number(lastsplash)+Number(1); }
                    else { nextsplash = "stop";}
                    
                    //Check current date is past the start date for current splash display period.
                    //It doesn't matter if it has hit the start date of next splashes, users are still
                    //consecutively shown the splash images, but the next one can only start after a 
                    //given date.
                    switch(Number(nextsplash))
                    {
                    case 1:
                        if ( Number(currentdate) > Number(Date.parse("Nov 1 2009 23:59:59")) )
                        {
                            setCookie('lastsplash',nextsplash,365);
                            setCookie('lastsplash_date', currentdate, 365);
                            return true;
                        } else { return false; }
                        break;
                    case 2:
                        if ( Number(currentdate) > Number(Date.parse("Nov 15 2009 23:59:59")) )
                        {
                            setCookie('lastsplash',nextsplash,365);
                            setCookie('lastsplash_date', currentdate, 365);
                            return true;
                        } else { return false; }
                        break;
                    case 3:
                        if ( Number(currentdate) > Number(Date.parse("Nov 29 2009 23:59:59")) )
                        {
                            setCookie('lastsplash',nextsplash,365);
                            setCookie('lastsplash_date', currentdate, 365);
                            return true;
                        } else { return false; }
                        break;
                    case 4:
                        if ( Number(currentdate) > Number(Date.parse("Dec 13 2009 23:59:59")) )
                        {
                            setCookie('lastsplash',nextsplash,365);
                            setCookie('lastsplash_date', currentdate, 365);
                            return true;
                        } else { return false; }
                        break;
                    default:
                        return false;    
                    }                           
                } else {
                    return false;
                }
        }
        
        $(document).ready(function(){
 
            if (checkCookie() == true)
            {
                //Get viewport width and decrease by 10%
                viewportWidth = ($(window).width() - ($(window).width() / 10));
                
                //To avoid quality loss, don't allow over 1024, which is the actual width of image
                if (viewportWidth > 1024) { viewportWidth = 1024; }
                
                //Calculate height based on original dimensions (Height must be 75% of width e.g. 1024 x 768)
                calculatedHeight = Math.round((viewportWidth - ((viewportWidth /10) * 2.5)));
                
                //Ensure calculated height doesn't extend beyond viewport height
                viewportHeight = $(window).height();
                
                if (calculatedHeight > viewportHeight) 
                    { 
                        //Calculated height would have extended beyond viewport... readjust height
                        calculatedHeight = (viewportHeight - (viewportHeight / 10));
                        
                        //As height has changed, width must now change in accordance with height
                        viewportWidth = ((calculatedHeight / 7.5) * 10); 
                    }
                
                //Finally.. round before putting into parameter
                viewportWidth = Math.round(viewportWidth);
                viewportHeight = Math.round(calculatedHeight);
                
                //Now that we have the final width/height of the overlay, we need to make the 'Enter' button, 
                //as an image map using co ordinates based on above
                
                //x1 - Distance from left edge of image to left edge of button - 80.56% of width
                x1 = Math.round((viewportWidth / 10) * 8.056);
                //y1 - Distance from top edge of imageto top edge of button   - 86.71% of height
                y1 = Math.round((viewportHeight / 10) * 8.671);
                //x2 - Distance from left edge of image to left edge of button - 92.87% of width
                x2 = Math.round((viewportWidth / 10) * 9.287);
                //y2 - Distance from top edge of image to top edge of button   - 91.02% of height
                y2 = Math.round((viewportHeight / 10) * 9.102);
                
                popup_html = '<img usemap="#enter_btn" width="'+viewportWidth+'" height="'+viewportHeight+'" border="0" src="/Themes/Suffolk01/SplashFiles/splash_' + getCookie('lastsplash') + '.jpg" alt="" />';
                popup_html = popup_html + '<map name="enter_btn"><area shape="rect" coords="'+x1+','+y1+','+x2+','+y2+'" href="#" onclick="tb_remove()" alt="Enter Site" /></map>';
                
                document.getElementById('splashContent').innerHTML = popup_html;
                tb_show("",'#TB_inline?height='+(viewportHeight+10)+'&width='+viewportWidth+'&inlineId=splashContent&modal=true');
                $("#TB_overlay").click(tb_remove);
            }
        });
