/***********************************************
* Fading Scroller- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/
var delay = 3000; //set delay between message change (in miliseconds)
var maxsteps=30; // number of steps to take to change from start color to endcolor
var stepdelay=40; // time in miliseconds of a single step
//**Note: maxsteps*stepdelay will be total time in miliseconds of fading effect
var startcolor= new Array(255,255,255); // start color (red, green, blue)
var endcolor=new Array(0,14,96); // end color (red, green, blue)

$(document).ready(function() {
	var internal = 0;
	function bunload() {
		if (internal != 1 && (!$.cookie('toner_exist') || $.cookie('toner_exist')!='true')) {
			$.cookie('toner_exist', 'true', { expires: 1 });
			window.onbeforeunload = null;
	  	window.open("http://tonerdirect.ca/form/pop_window02.html", "02", "'status=0,toolbar=0,menubar=0,resizable=1,scrollbars=1'");
		
		 }
	 }
	
	window.onbeforeunload = bunload;
   // Handler for .ready() called.
  var base_url = "http://www.tonerdirect.ca"; 
  var base_url1 = "http://tonerdirect.ca"; 
  $("a").click(function() {
  	var link_location = $(this).attr('href');
  	if($(this).attr('target')!="_blank")
  	{
	  	if (link_location.indexOf("http://")!=-1 && link_location.substr(0, base_url.length)!=base_url && link_location.substr(0, base_url1.length)!=base_url1) {               
	  			internal = 0;
					$.cookie('toner_popup', 'false', { expires: 1 });
					$.cookie('toner_exist', 'false', { expires: 1 });
	  	}
	  	else internal = 1;
  	} else internal = 1;
  });
  function show_popup(){       
  	if(!$.cookie('toner_popup') || $.cookie('toner_popup')!='true' )
  	{
  		$.cookie('toner_popup', 'true', { expires: 4 }); //alert("ok");
  		window.open("http://tonerdirect.ca/form/pop_window01.html", "01", "'status=0,toolbar=0,menubar=0,resizable=1,scrollbars=1'");
  	}else{ //alert("exist");
  		}
  };    
  function unloadMessage(){       
  	window.open("http://tonerdirect.ca/form/pop_window02.html", "02", "'status=0,toolbar=0,menubar=0,resizable=1,scrollbars=1'");
  };    
  window.setTimeout( show_popup, 5000 );
  //window.onbeforeunload = bunload;
  $(window).unload( function () { 
  	//window.open("http://tonerdirect.ca/form/pop_window02.html", "02", "'status=0,toolbar=0,menubar=0,resizable=1,scrollbars=1'");
  });

				
});

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

