var last_id = null;
var refresh = null;
var thumb_time = null;
var last_url = null;
var id = [];

var timerID = 0;
var tStart  = null;
var t = 0;

var myTimer = 0;
var thumb_id = null;

function peruse( iNum ) {
	if( !iNum ) { iNum = 0; }
	new Ajax.Updater( 'searchsection', '/index.php/search/peruse/'+region+'/'+type+'/'+iNum+'/'+query, { onLoading:function(request){ for(i=0; i<id.length; i++) { Element.hide( id[i] ) }; Element.show('searchloader_indicator') }, onComplete:function( request ){ last_id = null; Element.hide('searchloader_indicator') }, evalScripts:true, asynchronous:true } );
}

function getBasket( bWaterfall ) {

	new Ajax.Updater( 'basket', '/index.php/items/showbasket/', { onLoading:function(request){ Element.hide('basket_message'); Element.show('basketloader_indicator') }, onComplete:function(request){ Element.hide('basketloader_indicator'); if( bWaterfall ){ getWastebin( true ); } }, evalScripts:true, asynchronous:true } );
}

function getWastebin( bWaterfall ) { 

	new Ajax.Updater( 'wastebin', '/index.php/items/getWastebin/', { onLoading:function(request){ Element.show('wastebin'); }, onComplete:function(request){ if( bWaterfall ){ getPromo(); } }, evalScripts:true, asynchronous:true } );
}

function getPromo() { 

	new Ajax.Updater( 'stw_promo', '/index.php/items/getPromo/', { onLoading:function(request){ Element.show('stw_promo'); }, evalScripts:true, asynchronous:true } );
}

function getAdverts() {

	new Ajax.Updater( 'adverts', '/index.php/adverts/getadvert/'+query+'/'+region+'/'+type+'/', { evalScripts:true, asynchronous:true } );
}

function getPreview( id ) {

	new Ajax.Updater( 'detailspreview', '/index.php/items/getPreview/'+id+'/', { evalScripts:true, asynchronous:true } );
}

function refreshThumb( id ) {

	new Ajax.Updater( 'container'+id, '/index.php/items/refresh/'+id+'/', { onLoading:function(request){ Element.hide('refresh'); Element.show('preview_indicator') }, onComplete:function(request){ Element.show('refresh'); Element.hide('preview_indicator') }, evalScripts:true, asynchronous:true});
}

function getDetails( id ) {

	if( last_id != id.substr(2) || refresh > 0 ) {
	
		getPreview( id );
		last_id = id.substr(2);
		refresh = 0;
	}
}

function blendimage( divid, imageid, imagefile, millisec ) {

    var speed = Math.round(millisec / 100);
    var timer = 0;

    changeOpac(0, imageid);

	document.getElementById(imageid).src = imagefile;
	
	last_url = document.getElementById(imageid).src;
	
    for( i = 0; i <= 100; i++ ) {
        setTimeout( "changeOpac(" + i + ",'" + imageid + "')", (timer * speed) );
        timer++;
    }
}

function changeOpac(opacity, id) {
    var object = document.getElementById( id ).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

function UpdateTimer() {

    if( timerID ) {
		clearTimeout(timerID);
		clockID  = 0;
    }

    if(!tStart) {
		tStart   = new Date();
	}

    var tDate = new Date();
    var tDiff = tDate.getTime() - tStart.getTime();

	tDate.setTime(tDiff);
	t = tDate.getSeconds() +'.'+ tDate.getMilliseconds();
   	timerID = setTimeout( "UpdateTimer()", 10 );
}

function Start() {

	tStart   = new Date();
	timerID  = setTimeout("UpdateTimer()", 10);
}

function Stop() {

	if(timerID) {
		clearTimeout(timerID);
		timerID  = 0;
	}

	tStart = null;
}

function Reset() {

	tStart = null;
	t = 0;
}

function setLaunch() {

	Start();
}

function launch() {

	Stop();
	a = t.split('.',2);
	if( a[0] == 0 && a[1] < 200 ) {
		Reset();
		return true;
	}
	Reset();
	return false;
}