var php = new Array();
php["lib"] = 'php/layoutsections.php';

var gl_page=1;
var tmpLay = new Array();

function lay_get(p, category){
	JsHttpRequest.query(
		php["lib"],
		{
			action : 'getTemplate',
			page : p,
			cat : thisCat
		},
		function(result,errors){
			var tmpDiv = getId("box11_content");
			tmpDiv.innerHTML = '';
			var tmpDiv1 = getId("box12_content");
			tmpDiv1.innerHTML = '';
			var tmpIns = '';
			var tmpNum = result[0];
			var tmpStar = '';
			if (result[0]>5) tmpNum = 5;
			for (var i=1; i<=tmpNum; i++){
				tmpStar = '';
				tmpLay[i] = result[i];
				if (result[i]['voted']==0){
					for (j=0; j<Math.floor(result[i]['vote']); j++){
						tmpStar+= '<div class="star_on" id="star_'+result[i]['id']+'_'+(j+1)*1+'" onmouseout="voteIt('+result[i]['id']+', '+(j+1)*1+', 0, '+i+')" onmouseover="voteIt('+result[i]['id']+', '+(j+1)*1+', 1, '+i+')" onclick="voteThis('+result[i]['id']+','+(j+1)*1+')"></div>';
					};
					for (j=Math.floor(result[i]['vote']); j<5; j++){
						tmpStar+= '<div class="star_off" id="star_'+result[i]['id']+'_'+(j+1)*1+'" onmouseout="voteIt('+result[i]['id']+', '+(j+1)*1+', 0, '+i+')" onmouseover="voteIt('+result[i]['id']+', '+(j+1)*1+', 1, '+i+')" onclick="voteThis('+result[i]['id']+','+(j+1)*1+')"></div>';
					};
				}
				else{
					for (j=0; j<Math.floor(result[i]['vote']); j++){
						tmpStar+= '<div class="star_on"></div>';
					};
					for (j=Math.floor(result[i]['vote']); j<5; j++){
						tmpStar+= '<div class="star_off"></div>';
					};
				};
		var tmphref=new String();
		
		if(result[i]['url'] == null) tmphref="details.php?id="+result[i]['id'];
		else tmphref="layout/"+result[i]['url'];

				tmpIns+='<div class="template"><div class="templ_name">> '+result[i]['title']+'</div><div class="templ_img">';
                tmpIns+='<a href="'+tmphref+'"><img src="'+result[i]['thumnail']+'" alt="'+result[i]['title']+'" style="width: 130px; height: 137px;"/></a></div><div class="rate_stars" id="starvote_'+result[i]['id']+'" name="0">';
                tmpIns+=tmpStar+'</div><div class="more_link">> <a href="'+tmphref+'">more</a></div></div>';
            };
			var tmpBackBtn = '<div class="long_btn" style="padding-left: 5px; padding-right: 30px;"><a href="" onclick="gl_page--; lay_get(gl_page); return false;"><img src="images/long_btn.png" alt="more" /></a></div>';
            if (gl_page==1) tmpBackBtn = '';
			if (tmpIns!='')tmpDiv.innerHTML = tmpBackBtn+tmpIns;
			tmpIns = '';
			tmpStar = '';
            for (var i=6; i<=(result[0]>10?10:result[0]); i++){
				tmpStar = '';
				tmpLay[i] = result[i];
				if (result[i]['voted']==0){
					for (j=0; j<result[i]['vote']; j++){
						tmpStar+= '<div class="star_on" id="star_'+result[i]['id']+'_'+(j+1)*1+'" onmouseout="voteIt('+result[i]['id']+', '+(j+1)*1+', 0, '+i+')" onmouseover="voteIt('+result[i]['id']+', '+(j+1)*1+', 1, '+i+')" onclick="voteThis('+result[i]['id']+','+(j+1)*1+')"></div>';
					};
					for (j=result[i]['vote']; j<5; j++){
						tmpStar+= '<div class="star_off" id="star_'+result[i]['id']+'_'+(j+1)*1+'" onmouseout="voteIt('+result[i]['id']+', '+(j+1)*1+', 0, '+i+')" onmouseover="voteIt('+result[i]['id']+', '+(j+1)*1+', 1, '+i+')" onclick="voteThis('+result[i]['id']+','+(j+1)*1+')"></div>';
					};
				}
				else{
					for (j=0; j<result[i]['vote']; j++){
						tmpStar+= '<div class="star_on"></div>';
					};
					for (j=result[i]['vote']; j<5; j++){
						tmpStar+= '<div class="star_off"></div>';
					};
				};
				var tmphref=new String();
		
				if(result[i]['url'] == null) tmphref="details.php?id="+result[i]['id'];
				else tmphref="layout/"+result[i]['url'];
				
				tmpIns+='<div class="template"><div class="templ_name">> '+result[i]['title']+'</div><div class="templ_img">';
                tmpIns+='<a href="'+tmphref+'"><img src="'+result[i]['thumnail']+'" alt="'+result[i]['title']+'" style="width: 130px; height: 137px;" /></a></div><div class="rate_stars">';
//                tmpIns+='<div class="star_off"></div><div class="star_off"></div><div class="star_off"></div><div class="star_off">';
                tmpIns+=tmpStar+'</div><div class="more_link">> <a href="'+tmphref+'">more</a></div></div>';
            };
            var tmpForwBtn = '<div class="long_btn" style="padding-left: 5px;"><a href="" onclick="gl_page++; lay_get(gl_page); return false;"><img src="images/long_btn.jpg" alt="more" /></a></div>'
            if (result[0]<=10) tmpForwBtn = '';
            if (tmpIns!='')tmpDiv1.innerHTML = tmpIns+tmpForwBtn;
            tmpLay = result;
		},
		true
	);
};

function cat_get(){
	JsHttpRequest.query(
		php["lib"],
		{
			action : 'getCat'
		},
		function(result,errors){
			var tmpDiv = getId('box7_content');
			tmpDiv.innerHTML = '<div id="box7_title">Category <i> Loading...</i></div>';
			tmpIns = '<table border="0">';
			tmpNum = 6;
			if (result[0]<6) tmpNum = result[0];
			for (var i=1; i<=tmpNum; i++){
				if (i%2==1) tmpIns+='<tr>';
				tmpIns+='<td><div class="uc_content" style="width:120px;"><a href="layoutsections.php?cat='+result[i]['id']+'"><div class="uc_date">'+result[i]['name']+'</div></a></div></td>';
				if (i%2==0) tmpIns+='</tr>';
            };
            for (var i=7; i<=result[0]; i++){
				if (i%2==1) tmpIns+='<tr>';
				tmpIns+='<td><div class="uc_content" style="width:120px;"><a href="layoutsections.php?cat='+result[i]['id']+'"><div class="uc_date">'+result[i]['name']+'</div></a></div></td>';
				if (i%2==0) tmpIns+='</tr>';
            };
            tmpDiv.innerHTML = '<div id="box7_title">Category</div>'+tmpIns+'</table>';
		},
		false
	);
};

function layget_get(ordin){
	tmp_res = JsHttpRequest.query(
		php["lib"],
		{
			action : 'getLayget',
			ordnum : ordin
		},
		function(result,errors){
			result = result[1];
			getId("tutInstLay_btn").innerHTML='<a href="'+result['instal']+'"><img src="images/tut_inst_lay_btn.jpg" alt="tutorial on installing layout" /></a>';
			getId("codeBox_btn").innerHTML='<a href="'+result['code']+'"><img src="images/code_box_btn.jpg" alt="code box" /></a>';
			getId("layout").innerHTML='<img src="'+result['thumnail']+'" alt="'+result['title']+'" style="width: 542px; height: 561px;"/>';
		},
		true
	);
};

function coment_get(ordin){
	tmp_res = JsHttpRequest.query(
		php["lib"],
		{
			action : 'getComent',
			ordnum : ordin
		},
		function(result,errors){
			var tmpDiv=getId("userComments");
			tmpDiv.innerHTML='<div id="userCommentsTitle">User comments</div>';
			var tmpIns='';
			for (var i=1; i<=result[0]; i++){
				tmpIns+='<div class="comment"><div class="nickname">'+result[i]['nick']+'</div><div class="nickdatesep">|</div><div class="comment_date">'+result[i]['date']+'</div>';
				tmpIns+='<div class="comment_text">'+result[i]['text']+'</div></div>';
            };
            tmpDiv.innerHTML+=tmpIns;
		},
		false
	);
};

function anounce_get(ordin){
	JsHttpRequest.query(
		php["lib"],
		{
			action : 'getAnounce',
			ordnum : ordin
		},
		function(result,errors){
			result=result[1];
			getId("box1_img").innerHTML = '<img src="'+result['thumnail']+'" alt="'+result['title']+'" style="width: 301px; height: 229px;"/>';
			var tmpDiv = getId("box1_content");
//			tmpDiv.innerHTML='';
			var tmpIns = '';
			var tmpTip = (result['type']==1)?'lay':'wid';
			tmpIns+= '<h3>'+result['title']+'</h3><p>'+result['text']+'</p>';
			tmpIns+= '<a href="details.php?item='+tmpTip+'&id='+result['id']+'" class="moreBtn"><img src="images/more_btn.jpg" alt="more" /></a>';
            tmpDiv.innerHTML=tmpIns;
		},
		false
	);
};

function voteIt(layId, starId, act, tmpI){
//	alert (layId+'_'+starId+' - '+tmpLay[tmpI]['voted']);
	if (tmpLay[tmpI]['voted']=='0')
	{
	 	var tmpTrig = (act==1?starId:0);
		if (act==1){
			for (var ts=0; ts<tmpTrig; ts++)
				getId('star_'+layId+'_'+(ts+1)).className='star_on';
			for (var ts=tmpTrig; ts<5; ts++)
				getId('star_'+layId+'_'+(ts+1)).className='star_off';
		}
		else{
			for (var ts=0; ts<Math.floor(tmpLay[tmpI]['vote']); ts++)
				getId('star_'+layId+'_'+(ts+1)).className='star_on';
			for (var ts=Math.floor(tmpLay[tmpI]['vote']); ts<5; ts++)
				getId('star_'+layId+'_'+(ts+1)).className='star_off';
		};
	};
};

function voteThis(ordin, voteRate){
	JsHttpRequest.query(
		php["lib"],
		{
			action : 'voteThis',
			ordnum : ordin,
			vote : voteRate
		},
		function(result,errors){
			lay_get(gl_page, thisCat);
		},
		false
	);
};