/**
 * Change this variable to the time you would like the page (offer) to show for
 */
var OfferTime = 3600;

/**
 * Add in your redirect URL.  This is the URl that a visitor will be redirected to once the offer time has elapsed.
 */
var RedirectUrl = 'http://www.songsonpowerpoint.com';

/**
 * Change the cookie name below
 */
var cookie_name = 'songscookie0912233';


/**
 * Change the cookie length
 */
var cookie_life = 3600; // set in seconds ex. 60 is 1 minute,  3600 is 1 hour


/**
 * No need to change anything below this point
 */
var UTCStartStamp = 0;
var clockID = 0;

function writeIntoCounters( str ) {

	var counters = document.getElementsByTagName( 'span' );

	var i = 0;
	for ( ; i < counters.length; i++ )
	{
		var spanType = counters[ i ].getAttribute( 'type' );
		if ( spanType != null && spanType == 'countdown' )
		{
			counters[ i ].innerHTML = str;

		} 

	} 


	counters = document.getElementsByTagName( 'countdown' );

	i = 0;
	for ( ; i < counters.length; i++ )
	{

		if ( window.navigator.userAgent.indexOf('Firefox' ) >= 0 )
			counters[ i ].innerHTML = '<span>' + str + '</span>';
		else counters[ i ].outerHTML = '<span type="countdown">' + str + '</span>';

	} 

	return true;

} 


function UpdateClock() {

    if(clockID) {
	clearTimeout(clockID);
	clockID  = 0;
    }

    var currentStamp = getUTC();
    var dt = OfferTime - ( currentStamp - UTCStartStamp );

    if ( dt <= 0 )
    {
        if ( RedirectUrl.length > 0 )
        {
            writeIntoCounters( "" );
            window.location.href = RedirectUrl;
            return;
        }
        else 
        {
            writeIntoCounters( "Error: the RedirectUrl parameter is empty" );
            return;
        }
    }

    var daysD    = 60 * 60 * 24;
    var hoursD   = 60 * 60;
    var minutesD = 60;
    
    var daysLeft = parseInt( dt / daysD );
    dt -= daysLeft * daysD;

    var hoursLeft = parseInt( dt / hoursD );
    dt -= hoursLeft * hoursD;

    var minutesLeft = parseInt( dt / minutesD );
    dt -= minutesLeft * minutesD;

    var secondsLeft = dt;

    var counterString = '';
    var oneRe = new RegExp('1$');
    var one2Re = new RegExp('11$');

    if ( daysLeft > 0 )
    {
        counterString += daysLeft;
        if ( daysLeft.toString().match( oneRe ) && !daysLeft.toString().match( one2Re ) )
            counterString += ' day ';
        else counterString += ' days ';

    } 

    if ( hoursLeft > 0 )
    {
        counterString += hoursLeft;
        if ( hoursLeft.toString().match( oneRe ) && !hoursLeft.toString().match( one2Re ) )
            counterString += ' hour ';
        else counterString += ' hours ';

    } 

    if ( minutesLeft > 0 )
    {
        counterString += minutesLeft;
        if ( minutesLeft.toString().match( oneRe ) && !minutesLeft.toString().match( one2Re ) )
            counterString += ' minute ';
        else counterString += ' minutes ';

    } 

    counterString += secondsLeft;
    if ( secondsLeft.toString().match( oneRe ) && !secondsLeft.toString().match( one2Re ) )
        counterString += ' second ';
    else counterString += ' seconds ';

    counterString += ' ';

    writeIntoCounters( counterString );

    if ( dt >= 0 )
       clockID = setTimeout("UpdateClock()", 500);

}


function getUTC(){

   var currentDate = new Date();
   var UTCStamp = Date.UTC( currentDate.getUTCFullYear(), currentDate.getUTCMonth(), currentDate.getUTCDate(), currentDate.getUTCHours(), currentDate.getUTCMinutes(), currentDate.getUTCSeconds(), currentDate.getUTCMilliseconds());

   UTCStamp = Math.round( UTCStamp / 1000 );

   return UTCStamp;

}


function StartClock(){

   var UserVisitTime = GetCookie( cookie_name );

   if ( !UserVisitTime || UserVisitTime.length == 0 )
   {
      UTCStartStamp = getUTC();

      SetCookie( cookie_name, UTCStartStamp, cookie_life, '/' );

   } 
   else 
   {
      UTCStartStamp = UserVisitTime;

   } 

   if (UTCStartStamp) {
	clockID = setTimeout("UpdateClock()", 500);
   }
}


function KillClock() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }
}


function SetCookie( name, value, expires, path, domain, secure) {

	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires )
		expires = expires * 1000;

	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 GetCookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
		( name != document.cookie.substring( 0, name.length ) ) )

		return null;

	if ( start == -1 ) 
		return null;

	var end = document.cookie.indexOf( ";", len );

	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );

} 
	

function DeleteCookie( 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";

} 