$(document).ready(function(){ // fires when dom says it's ready for action

var cookiesEnabled = checkCookieWrite(); // this function checks whether the browser is accepting cookies (IE is a bit flaky on this)

  if (cookiesEnabled == true)
  {
    if (parseInt($.cookie("checkCookieWrite")) ==1 ) { // this is another fix, which attempts to patch the IE problem, in that it only fires the first time the cookie write test occurs, the function increments the value of the cookie each time so multiple fires within the same session should be impossible.

    var initCookie = $.cookie("survey"); // reads-in the current survey cookie value, if it exists

      if ((initCookie != 'session') && (initCookie != 'never') && (initCookie != 'completed')) // check that we're not barred from popping-up
      {  
        window.setTimeout(function() {go();}, 2000);
      };
   };
};

  function go() {
        $.fn.colorbox({href:"#survey_message", width:scWidth(), height:scHeight(), inline:true, scrolling:true,overlayClose:false, initialWidth:screen.width, initialHeight:screen.height}); // create a colorbox object
	};
  });

  function scHeight() {
    var height = ((screen.height /100) *60);
	if (height < 450)
	{
		height = 450;// sets a minimum screen height which is a fix for mobile browsers
	}
	return height;
  };

  function scWidth() {
    var width = ((screen.width / 100) *60);
	if (width < 600)
	{
		width = 600;// sets a minimum screen width which is a fix for mobile browsers
	}
	return width;
  };

  function surveyClick(value) {
	switch(value) {
       case 'take': // pops up the survey and sets a persistent cookie
         popup();
         $.cookie("survey", "completed", { expires: 90, path: '/' });
         break;
       case 'later': // sets a session cookie
           $.cookie("survey", "session", {path: '/'} );
         break;
       case 'never': // sets a persistent cookie
	     $.cookie("survey", "never", { expires: 90, path: '/' });
         break;
    }
    $.fn.colorbox.close(); // closes the colorbox object in all instances
};

  function popup() {
    win=window.open('http://web.hullcc.gov.uk/surveys/hcc_internet/web_survey/web_survey.htm','name','location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1'); 
    win.blur(); // blurs the pop up
  };

  function checkCookieWrite() {
	var returnVal = false;
    var value = parseInt ($.cookie("checkCookieWrite")); // convert cookie value to an integer

      if (isNaN(value)) // if cookie value doesn't exist
      {
        value = 0; // set it to zero
      };

    value ++; //increment the current cookie value
    $.cookie("checkCookieWrite", value, {path: '/' }); // write the new value to the cookie

      if ($.cookie("checkCookieWrite"))
      {
	    returnVal = true; // returns true when the cookie write was successful
      }; 
    return returnVal;
  };
