Ext.onReady(function(){
    Ext.QuickTips.init();
	
	var ds = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: rootDir+'ajax/products/json/',
            method: 'GET'
        }),
        reader: new Ext.data.JsonReader({
            root: 'results',
            totalProperty: 'totalCount'
        }, [
            {name: 'name', mapping: 'name'},
            {name: 'short_desc'},
            {name: 'img_src'},
            {name: 'price'},
            {name: 'category'},
            {name: 'link'},
            {name: 'total'},
            {name: 'no'}
        ])
    });

    // Custom rendering Template
    var resultTpl = new Ext.XTemplate(
        '<tpl for=".">',
        '<tpl if="no &lt; 1"><div class="search-item"><table border="0" align="center" cellpadding="0" cellspacing="0"><tr><td height="20">Show all {total} product(s)&nbsp;</td><td><img src="images/enter_key.gif"></td></tr></table></div></tpl>',
        '<tpl if="no &gt; 0"><div class="search-item">',
        '<table width="465" border="0" cellspacing="0" cellpadding="0">',
        	'<tr>',
        		'<td width="75" align="left" valign="top"><img src="{img_src}" alt="{name}" border="0" style="border:solid 1px #CCCCCC;" /></td>',
        		'<td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">',
        			'<tr>',
        				'<td colspan="2" align="left" style="padding-bottom:3px; padding-top:2px;"><h3>{name}</h3></td>',
        			'</tr>',
        			'<tr><td colspan="2" align="left" style="padding-bottom:3px; padding-top:2px; color:#999999;">{short_desc}</td></tr>',
        			'<tr>',
	        			'<td align="left"><div id="price_good" style="font-size:11px; font-weight:normal;">{price}</div></td>',
	        			'<td align="right" style="color:#777777;">{category}</td>',
        			'</tr>',
        		' </table></td>',
        	'</tr>',
        '</table>',
        '</div></tpl></tpl>'
    );
    
    var search1 = new Ext.form.ComboBox({
        store: ds,
        displayField:'name',
        typeAhead: false,
        forceSelection: false,
        validateOnBlur: false,
        minChars: 3,
        loadingText: 'Searching...',
        emptyText: 'Type a product...',
        width: 170,
        listWidth: 500,
        itemSelector: 'div.search-item',
        pageSize:50,
        maxHeight: 400,
        hideTrigger:true,
        tpl: resultTpl,
        applyTo: 'q',
        onSelect: function(record){ // override default onSelect to do redirect
        	if (parseInt(record.data.no)<=0) {
        		window.location = rootDir+'Products/?q='+search1.getValue();
        	} else {
            	window.location = record.data.link;
        	}
        }
    });
	
});



var timerform;

var speed=1000;

function dotimer(Sec,Min,Hour) {
//alert(Sec+' | '+Min+' | '+Hour);
today=new Date()
tday = today.getDate();
tmon = today.getMonth();
tyear = today.getYear();

today=new Date(tyear,tmon,tday,Hour,Min,Sec);
slutsec=today.getSeconds()
slutmin=today.getMinutes()
sluttim=today.getHours()

sluta=(slutsec) + 60 * (slutmin) + 3600 * (sluttim)

//diff=sluta - starta
diff=starta - sluta

if (diff<=0) {
	loadAjax('grab_best',rootDir+'ajax/products/grab-best/','');
	return;
}

tim=Math.floor(diff / 3600)

min=Math.floor((diff / 3600 - tim) * 60)

sek=Math.round((((diff / 3600 - tim) * 60) - min) * 60)

timerform='';
if (tim>0) timerform=tim + ':'
if(min<10) timerform+='0'
timerform+=min + ':'
if(sek<10)timerform+='0'
timerform+=sek

if (ge('stopclock')) {
	ge('stopclock').innerHTML = timerform;
}

window.setTimeout("dotimer("+(slutsec+1)+","+slutmin+","+sluttim+")",speed);

}

var starta;

function Timer(startsek,startmin,starttim,Sec,Min,Hour) {

starta=(startsek) + 60 * (startmin) + 3600 * (starttim)

dotimer(Sec,Min,Hour);

}
