/// <reference path="~/script/Debug/jQuery/jQuery.intellisense.js" />

// To use this page, include these files into the page to be rated:
//      <script type="text/javascript" language="javascript" src="/script/Debug/jQuery/shadowbox-2.0.js"></script>
//      <script type="text/javascript" language="javascript" src="/css/ShadowBoxSkins/diamonds/skin.js"></script>
//      <script type="text/javascript" language="javascript" src="/ctl/pagerater.js"></script>

// If you wish to create a link onto the page, include a link on the page with an id of "ratePageLink" and include this javascript:
//      <script language="javascript" type="text/javascript">
//          $("#ratePageLink").click(function() {
//              Shadowbox.open({
//                  player: 'iframe',
//                  content: '/ctl/pagerater.aspx',
//                  height: 325,
//                  width: 625
//              });
//          });
//      </script>

Shadowbox.loadSkin('diamonds', '/css/ShadowboxSkins');

$(document).ready(function() {
    Shadowbox.init({
        skipSetup: true
    });
    var counter;
    var locationHref = parent.window.location.href;
    locationHref = locationHref.substring(locationHref.lastIndexOf("/") + 1, locationHref.lastIndexOf("."))
    if (getCookie('VisitCount_' + locationHref) == undefined) {
        counter = 0;
    } else {
        counter = getCookie('VisitCount_' + locationHref);
    }
    counter++;
    setCookie('VisitCount_' + locationHref, counter);

    $.ajax({
        type: "POST",
        url: "/ctl/pagerater.aspx/GetAuthentication",
        contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",
        success: function(info) {
            if (info.d) {
                $("#ratePageLink").show();
//                if (getCookie('PollTaken') != "True" && getCookie('VisitCount_' + locationHref) == "15") {
//                    Shadowbox.open({
//                        player: 'iframe',
//                        content: '/ctl/pagerater.aspx',
//                        height: 375,
//                        width: 625
//                    });
//                }
            } else {
                $("#ratePageLink").hide();
            }
        }
    });
 //  Add this back if we start having errors
//, error: function(req, msg, err) { ShowError(req, msg, err, 'GetAuthentication'); }
});

function ShowError(req, msg, err, caller)
   {
      if (req.responseText.length > 0) 
     	   var xml = '<?xml version="1.0"?>\n' +
   		   '<error>\n' +
	   	   '<name>' + req.status + '</name>\n' +
		      '<message>' +
		      '<![CDATA[' + 
		      req.responseText + 
		      '\n\n' +		      
            ']]>\n' +
		      '</message>\n' +
		      '<location>' + caller + '</location>\n' +
		      '</error>';	
       else
     	   var xml = '<?xml version="1.0"?>\n' +
   		   '<error>\n' +
	   	   '<name>' + req.status + '</name>\n' +
		      '<message>' +
		      '<![CDATA[' + 
		         'Response Text is nothing' + 
		      '\n\n' +		      
            ']]>\n' +
		      '</message>\n' +
		      '<location>' + caller + '</location>\n' +
		      '</error>';	
         
            $.ajax({
               type: "POST",
               url: "/err/JSError.aspx",
               data: xml,
               contentType: "text/xml"
            });
      //$('#errMsg').html(req.responseText);
      //$('#divError').show();
   }

function setCookie(idCookie, value) {
    var cookie_date = new Date();
    cookie_date.setFullYear(cookie_date.getFullYear() + 2);
    document.cookie = idCookie += "=" + value + "; expires=" + cookie_date.toGMTString();
}

function getCookie(idCookie) {
    var results = document.cookie.match(idCookie + '=(.*?)(;|$)');
    if (results)
        return (unescape(results[1]));
    else
        return null;
}
