/*
Grip Traffic API 3.0
10/2011
wes@griptraffic.com

Usage:

<!-- Embedded GripTraffic: Insert the script below at the point on your page where you want the Grip Traffic link to appear -->
<link rel="stylesheet" type="text/css" href="http://www.griptraffic.com/_css/griptraffic_api3.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.griptraffic.com/_js/fancybox/jquery.fancybox-1.3.4.js"></script>
<script type="text/javascript" src="http://www.griptraffic.com/_js/griptraffic_api3.js"></script>
<script type='text/javascript' src='http://server3gateway.clickandchat.com/include.js?domain=www.griptraffic.com'></script>
<SCRIPT>
var msite = "www.griptraffic.com";
var h_string = "";
if(typeof sWOTrackPage=='function')sWOTrackPage();
h_string = prepGrip(msite);
document.write(h_string);
$j(document).ready(function()
{
	gripChat(msite);
});
</SCRIPT>
<!-- END OF GRIP TRAFFIC EMBEDDED CODE -->

*/

var $j = jQuery.noConflict();

function gripChat(msite)
{
	var debug = 0; // set this to 1 to always show the popup

	if(	!Get_Cookie( 'beenthere' ) || debug > 0 )
	{
		/*
		$j.get('http://www.griptraffic.com/griptraffic_api3.php?site='+msite, function(data) {
			$j("#gripd").html(data);
		});
		*/
		
		var myw = window.innerWidth;
		var myh = window.innerHeight;
		var truw = 100-(((myw-520)/myw)*100);
		var truh = 100-(((myh-320)/myh)*100);
		var w = truw + "%";
		var h = truh + "%";
        w = 300+"px";
        h = 200+"px";
		
		$j("#box_data").fancybox({
			'width'				: w,
			'height'			: h,
			'autoScale'			: true,
			'transitionIn'		: 'elastic',
			'transitionOut'		: 'elastic',
			'showNavArrows'		: false,
			'type'				: 'inline'
		}).trigger('click');		
	}

	// name, value, expires, path, domain, secure
	Set_Cookie( 'beenthere', 'donethat', '', '/', '', '' );
}


function prepGrip(msite)
{
	var html_string = "";
    
    html_string = '<a id="box_data" href="#gripd" STYLE="display: none;">...</a>';
    html_string = html_string + '<DIV STYLE="display: none;">';
    html_string = html_string + '    <DIV STYLE="padding: 0px; width: 450px; height: 260px; background-color: #ffffff;" ID="gripd">';
    html_string = html_string + '		<IFRAME SRC="http://www.griptraffic.com/griptraffic_api3.php?site='+msite+'" COLS="0" ROWS="0" scrolling="no" STYLE="width: 440px; HEIGHT: 190px; border-width: 0px;"></IFRAME>';	
    html_string = html_string + '		<DIV STYLE="padding: 20px; font-face: Arial; ">';
    html_string = html_string + '	        <a href="https://server3.clickandchat.com/chat/chatstart.htm?domain='+msite+'" onClick="javascript:window.open(\'https://server3.clickandchat.com/chat/chatstart.htm?domain='+msite+'\',\'wochat\',\'width=484,height=361\');return false;"><IMG SRC="http://www.griptraffic.com/_img/yes.jpg" BORDER=0></a>';
    html_string = html_string + '	        <a HREF="#" onClick="parent.looseGrip();"><IMG SRC="http://www.griptraffic.com/_img/nothanks.jpg" BORDER=0></a>';
    html_string = html_string + '		</DIV>';
    html_string = html_string + '    </DIV>';
    html_string = html_string + '</DIV>';
    
    return html_string;
}

function looseGrip()
{ 
	jQuery.fancybox.close(); 
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) 
{
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function Delete_Cookie( name, path, domain ) 
{
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


