var sp_playing = sp_get_cookie('sp_playing');
var sp_control = document.getElementById('sp_control');
//var sp_imgPlay = 'http://everyday.demo.sanookonline.co.th/star/gallery/imgs/button_play.gif';
var sp_imgPlay = 'http://static.sanook.com/share/story_picture/button_play.gif';
//var sp_imgPause = 'http://everyday.demo.sanookonline.co.th/star/gallery/imgs/button_stop.gif';
var sp_imgPause = 'http://static.sanook.com/share/story_picture/button_stop.gif';

if(sp_playing > 0) {
	sp_slideshowing();
}
	
function sp_slideshowing() {
		sp_playing = sp_get_cookie('sp_playing');
		if(sp_playing > 0) {
			sp_control.src = sp_imgPause;

			var sp_control_next = document.getElementById('sp_control_next');
			setTimeout(function() {
				sp_playing = sp_get_cookie('sp_playing');
				if(sp_playing>0) {
					window.location = sp_control_next.href;
				}
			},sp_playing);
		}
}

function sp_slideshow() {
	if(sp_control.src == sp_imgPlay) {
		sp_playing = true;
		sp_control.src = sp_imgPause;
	}
	else {
		sp_playing = false;
		sp_control.src = sp_imgPlay;
	}

	if(sp_playing) {
		sp_set_cookie('sp_playing',6000);
		sp_slideshowing();
	}
	else {
		sp_set_cookie('sp_playing',0);
	}
}


function sp_set_cookie( name, value) 
{


var expires='';
	var path = '';
	var domain = '';
	var 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() {
		$.cookie(COOKIE_NAME, value, { path: '/', expires: 10 }); //10days
	});
	*/
	return true;
}

function sp_get_cookie( 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 ) );
/*

	var value = '';
	$(function() {
		//alert($.cookie(COOKIE_NAME));
		value = $.cookie(COOKIE_NAME);
	});
	return value;
	*/
}