/**
 * @name: 		main.js
 * @author: 	Albert Harounian
 * @since:		March 31, 2008
 * @desc:		Main JS file
 */

window.onload = function(){
	initDoc();
}

function initDoc(){
	im_center.sizesArr['msg_screen'] 			= {width: 340, height: 180};
	im_center.sizesArr['register_screen'] 		= {width: 400, height: 330};
	im_center.sizesArr['video_upload_screen'] 	= {width: 400, height: 400};
	im_center.sizesArr['pic_upload_screen'] 	= {width: 400, height: 400};
	im_center.sizesArr['gallery_image_screen'] 	= {width: 536, height: 360};
	im_center.sizesArr['yad2_newProduct_screen']= {width: 400, height: 440};
	im_center.sizesArr['watch_video_screen']	= {width: 536, height: 623};
	im_center.sizesArr['lost_pass_screen']		= {width: 390, height: 200};
	im_center.sizesArr['ticker_screen'] 		= {width: 400, height: 350};
	im_center.sizesArr['add_comment_screen'] 	= {width: 390, height: 300};
	im_center.sizesArr['form_err_message'] 		= {width: 300, height: 200};
	im_center.sizesArr['contact_screen'] 		= {width: 400, height: 360};
	users.isLoggedIn = _isLoggedIn;
	im_ticker.init("tickerDiv", "right", true);
	clearSiteCover();
}

document.onkeyup = function(e){
	key = (document.all ? event.keyCode : e.which );
	if (key==27){
		im_center.close();
	}
}

var GLOBAL = {
	'yesNo_str' : '<p><table border="0" align="center" width="100%"><tr><td align="center"><img src="/_media/images/layout/popup/yes.jpg" alt="כן" title="כן" class="pointer" onclick="javascript: {CONFIRM_FUNC};" /></td><td align="center"><img src="/_media/images/layout/popup/no.jpg" alt="לא" title="לא" class="pointer" onclick="javascript: im_center.close();" /></td></tr></table></p>',
	'doRegister_link' : '<p><span class="pointer underline" onclick="javascript: im_center.close();users.register();">לחץ כאן להרשמה</span></p>',
	'siteCoverTimerID': ''
};

function frmSendVideo(){
	if(!users.isLoggedIn){
		im_center.show('msg_screen',
			im_center.templates.compile({
				'TITLE' : "העלאת וידאו",
				'CONTENT' : "אופציה זו שמורה למשתמשים רשומים בלבד"+GLOBAL.doRegister_link
			})
		);
		return false;
	}
	function done(data){
		im_center.show('video_upload_screen',
			im_center.templates.compile({
				'TITLE' : "הוספת וידאו",
				'CONTENT' : data
			})
		);
	}
	im_ajax.send('video.upload.ajax.php', done);
}

function sendVideo(){
	function done(data){
		im_center.show('msg_screen',
			im_center.templates.compile({
				'TITLE' : "העלאת וידאו",
				'CONTENT' : data
			})
		);
		if(data.indexOf('<ok/>')>-1){
			im_gui.obj('frmSendVideo').reset();
		}
	}
	submitAjaxForm('frmSendVideo', 'video.upload.ajax.php', done);
	return false;
}

function frmSendComment(type, id){
	function done(data){
		im_center.show('add_comment_screen',
			im_center.templates.compile({
				'TITLE' : "הוספת תגובה",
				'CONTENT' : data
			})
		);
	}
	im_ajax.send('comments.add.ajax.php', done, "type="+type+"&item_id="+id);
}

function sendComment(){
	function done(data){
		im_center.close();
		im_center.show('msg_screen',
			im_center.templates.compile({
				'TITLE' : "הוספת תגובה",
				'CONTENT' : data
			})
		);
	}
	submitAjaxForm('frmSendComment', 'comments.add.ajax.php', done);
	return false;
}

function getComments(type, id){
	function done(data){
		im_gui.misc.innerData('comments', data);
		im_gui.obj('all_commentsBtn').onclick = function(){};
	}
	im_ajax.send('comments.get.ajax.php', done, "type="+type+"&item_id="+id);
}

function frmSendPicture(){
	if(!users.isLoggedIn){
		im_center.show('msg_screen',
			im_center.templates.compile({
				'TITLE' : "העלאת תמונה",
				'CONTENT' : "אופציה זו שמורה למשתמשים רשומים בלבד"+GLOBAL.doRegister_link
			})
		);
		return false;
	}
	im_center.show('pic_upload_screen', '<iframe frameborder="0" src="/uploadpic" width="400" height="400" allowTransparency="true"></iframe>');
}

function showPicture(imgPath, w, h, imgTitle){
	im_center.close();
	im_center.show({'width': w+55, 'height': h+130},
		im_center.templates.compile({
			'TITLE' : imgTitle,
			'CONTENT' : '<center><img src="'+imgPath+'" title="'+imgTitle+'" /></center>'
		},(typeof(imgTitle)=='undefined' || imgTitle=='' ? 'galleryImg' : ''))
	);
}

function yad2NewProduct(){
	if(!users.isLoggedIn){
		im_center.show('msg_screen',
			im_center.templates.compile({
				'TITLE' : "העלאת תמונה",
				'CONTENT' : "אופציה זו שמורה למשתמשים רשומים בלבד"+GLOBAL.doRegister_link
			})
		);
		return false;
	}
	im_center.show('yad2_newProduct_screen', '<iframe frameborder="0" src="/uploadyad2product" width="400" height="400" allowTransparency="true"></iframe>');
}

function watchVideo(title, content, code){
	if(code!=''){
		im_center.show('watch_video_screen',
			im_center.templates.compile({
				'TITLE' : title,
				'CONTENT' : '<object width="480" height="390"><param name="movie" value="http://www.youtube.com/v/'+code+'"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+code+'" type="application/x-shockwave-flash" wmode="transparent" width="480" height="390"></embed></object>'+'<p>'+content+'</p>'
			})
		);
	}
}

function showTickerContent(title, content){
	im_center.close();
	im_center.show('ticker_screen',
		im_center.templates.compile({
			'TITLE' : im_gui.string.limit_str(title, 28),
			'CONTENT' : content
		})
	);
}

function doGoogleSearch(frm){
	frm.q.value+=" site:planesandmodels.co.il";
	window.setTimeout("cleanGoogleForm()", 5);
	return true;
}

function cleanGoogleForm(){
	var q=im_gui.obj('frmGoogleSearch').q;
	var qv=q.value;
	qv=qv.replace(' site:planesandmodels.co.il', '');
	q.value=qv;
}

function showYad2Product(title, content){
	im_center.close();
	im_center.show('yad2Product_screen',
		im_center.templates.compile({
			'TITLE' : im_gui.string.limit_str(title, 28),
			'CONTENT' : content
		})
	);
}

function frmContact(){
	function done(data){
		im_center.close();
		im_center.show('contact_screen',
			im_center.templates.compile({
				'TITLE' : "יצירת קשר",
				'CONTENT' : data
			})
		);
	}
	im_ajax.send("contact.ajax.php", done);
}

function sendContact(){
	function done(data){
		im_center.close();
		im_center.show('msg_screen',
			im_center.templates.compile({
				'TITLE' : "יצירת קשר",
				'CONTENT' : data
			})
		);
	}
	submitAjaxForm("frmContact", "contact.ajax.php", done);
}

function contactToyad2ProductOwner(ownerID, productID){
	function done(data){
		im_center.close();
		im_center.show('contact_screen',
			im_center.templates.compile({
				'TITLE' : "יצירת קשר עם בעל הפריט",
				'CONTENT' : data
			})
		);
	}
	im_ajax.send("contact.yad2product.ajax.php", done, "ownerID="+ownerID+"&productID="+productID);
}

function contactToyad2ProductOwner_send(ownerID){
	function done(data){
		im_center.close();
		im_center.show('contact_screen',
			im_center.templates.compile({
				'TITLE' : "יצירת קשר עם בעל הפריט",
				'CONTENT' : data
			})
		);
	}
	submitAjaxForm("frmContact", "contact.yad2product.ajax.php", done);
}

function delYad2Product(productID){
	im_center.show('msg_screen', 
		im_center.templates.compile({
			'TITLE' : "מחיקת פריט יד שנייה",
			'CONTENT' : "האם אתה בטוח שברצונך למחוק פריט זה?"+GLOBAL.yesNo_str.replace('{CONFIRM_FUNC}','delYad2Product_confirmed('+productID+');')
		})
	);
}

function delYad2Product_confirmed(productID){
	function done(data){
		im_center.close();
		im_center.show('msg_screen',
			im_center.templates.compile({
				'TITLE' : "מחיקת פריט יד שנייה",
				'CLOSEBTN' : "im_gui.misc.gotoURL('/secondhand/')",
				'CONTENT' : data
			}, 'customCloseBtn')
		);
	}
	im_ajax.send("del.yad2product.ajax.php", done, "productID="+productID);
}

function siteCover(){
	var bodySize = im_gui.getBodyDim();
	with(im_gui.obj("siteCoverDiv").style){
		width = bodySize.offsetWidth-20+"px";
		height = bodySize.offsetHeight+"px";
	}
	GLOBAL.siteCoverTimerID = window.setTimeout("siteCover()", 250);
}

function clearSiteCover(){
	window.clearTimeout(GLOBAL.siteCoverTimerID);
	im_gui.obj("siteCoverDiv").style.display = "none";
}