var modelProfileVisible = null;
var specailImg = null;
function showModelPopup(data, image) {
	hidePopup();
	if (typeof data == 'object') {
		imageCell = '';
		if (typeof image != 'undefined') {
			imageCell = "<td rowspan='7' style='width:auto !important;'><img src=\"/files/"+image+"\" /></td>";
		}

		setOnScreen();

		modelProfileVisible = true;
		populatePopup('<table class="modelInfo"><tr>'+imageCell+'<th>Height:</th><td>'+data.height+'</td><th>Age:</th><td>'+data.age+'</td></tr>'+
		'<tr><th>Weight:</th><td>'+data.weight+' lb</td><th>Ethnicity:</th><td>'+data.ethnicity+'</td></tr>'+
		'<tr><th>Chest:</th><td>'+data.chest+' '+data.cup+'</td><th>Eye Color:</th><td>'+data.eye_col_id+'</td></tr>'+
		'<tr><th>Waist:</th><td>'+data.waist+'"</td><th>Hair Color:</th><td>'+data.hair_col_id+'</td></tr>'+
		'<tr><th>Hips:</th><td>'+data.hips+'"</td><th>Hair Length:</th><td>'+data.hair_length+'</td></tr>'+
		'<tr><th>Skin Color:</th><td>'+data.skin_col_id+'</td><th>Dress size:</th><td>'+data.dress_size+'</td></tr>'+
		'<tr><th>Location:</th><td class="capitalise">'+data.location+'</td><th>Shoe size:</th><td>'+data.shoe_size+'</td></tr>'+
		'</table>', true);
	}
}

function hideModelPopup() {
	modelProfileVisible = false;
	hidePopup();
}

var otherProfileVisible = null;
function showOtherPopup(data, image) {
	hidePopup();
	imageCell = '';
	if (typeof image != 'undefined') {
		imageCell = "<td rowspan='6' style='width:auto !important;'><img src=\""+(image.match("http") ? "" : "/files/")+image+"\" /></td>";
	}else{
		image = '';
	}

	setOnScreen();

	modelProfileVisible = true;
	
	if (typeof data == 'object') {
		populatePopup('<table class="modelInfo"><tr>'+imageCell+(image!=''?'<td colspan="2">&nbsp;</td></tr><tr>':'')+'<th>Name:</th><td class="capitalise">'+data.name+'</td></tr>'+
		(data.profession_name!=''?'<tr><th>Profession:</th><td>'+data.profession_name+'</td></tr>':"")+
		(data.location!=''?'<tr><th>Location:</th><td class="capitalise">'+data.location+'</td></tr>':"")+
		(data.age!=''?'<tr><th>Age:</th><td>'+data.age+'</td></tr>':"")+
		(image!=''?'<tr><td colspan="2">&nbsp;</td></tr>':'')+'</table>', true);
	}
	else if (image != '') {
		populatePopup('<table><tr><td><img src=\"/files/"+image+"\" /></td></tr></table>', true);
	}
}

function hideOtherPopup() {
	otherProfileVisible = false;
	hidePopup();
}

function showProfileImg(type, image, data) {
	if (type=='model') {
		showModelPopup(data, image);
	}else if (type == 'other') {
		showOtherPopup(data, image);
	}
}

function loadTfpPopup(userId) {
	//Hide other popup types to avoid bugs;
	hideModelPopup();
	hideOtherPopup();
	hidePopup();

	populatePopup('<h6 style="margin: 0;"><span>Send TFP request</span></h6><div id=\"minifolioContent\" style=\"width: 550px; text-align: center;\">Loading minifolio galleries...');

	new Ajax.Request('/minifolios/tfp/'+userId, {method: 'get', onSuccess: doLoadMinifolio});

	showCenteredPopup(true);
}

var doLoadMinifolio = function (t) {
	$('minifolioContent').innerHTML = t.responseText;
}

function sendTfpRequest(id, to_user) {
	popupLocked	 = true;
	new Ajax.Request('/minifolios/sendtfp/'+id+'/'+to_user, {method: 'get', onSuccess: doLoadMinifolio});
}

function refreshAfterTfpRequest() {
	popupLocked	 = false;
	hideDimmer(true);
	window.location.reload(true);
}

// book
function applyCasting(userId, castingId) {
	//Hide other popup types to avoid bugs;
	hideModelPopup();
	hideOtherPopup();
	hidePopup();

	populatePopup('<h6 style="margin: 0;"><span>Apply</span></h6><div id=\"bookContent\" style=\"width: 550px; text-align: center;\">Loading...');

	new Ajax.Request('/books/book/'+userId+'/'+castingId, {method: 'get', onSuccess: doLoadBook});

	showCenteredPopup(true);
}

var doLoadBook = function (t) {
	$('bookContent').innerHTML = t.responseText;
}

function sendBook(id, castingId) {
	new Ajax.Request('/books/sendbook_byid/'+id+'/'+castingId, {method: 'get', onSuccess: doLoadBook});
}

var popImg = null;
function popMinifolioImg(img) {
	//Hide other popup types to avoid bugs;
	hideModelPopup();
	hideOtherPopup();

	showDimmer();
	populatePopup('<img src="'+getImage('progress')+'" />');
	showCenteredPopup();

	popImg = new Image();
	popImg.onload = popMinifolioImgOnload;
	popImg.src = '/files/'+img;
}

function popMinifolioImgOnload() {
	//alert('loaded');
	populatePopup('<img src="'+popImg.src+'" alt="big image" style="cursor: pointer; position:relative" onclick="hideDimmer(true)" />');
	showCenteredPopup();
}

function setErrorMessage(innerText) {
	errorMessage = innerText;
}

function loadEditPicPopup(picID) {
	//Hide other popup types to avoid bugs;
	hideModelPopup();
	hideOtherPopup();
	hidePopup();

	populatePopup('<h6 style="margin: 0;"><span>Edit picture</span></h6><div id=\"editPicContent\" style=\"width: 350px; text-align: center;\">Loading picture data...');

	new Ajax.Request('/photos/edit_pic/'+picID, {method: 'get', onSuccess: doLoadEditPic});

	showCenteredPopup(true);
}

var doLoadEditPic = function (t) {
	$('editPicContent').innerHTML = t.responseText;
	reCenterPopup();
	//popupLocked = true;
}

function hideEditPicPopup()
{
	hideDimmer(true);
}

function sendPictureData()
{
	var pic_id = document.getElementById('pic_id').value;
	var name = document.getElementById('name').value;
	var aperture = document.getElementById('aperture').value;
	var iso = document.getElementById('iso').value;
	var focal_length = document.getElementById('focal_length').value;
	var camera = document.getElementById('camera').value;
	var model = document.getElementById('model').value;
	if(document.getElementById('description'))
		var description = document.getElementById('description').value;
	else
		var description = '';
	var categories = document.getElementById('categories').value.split(',');
	var cats = '';
	for(i = 0; i < categories.length; i++)
	{
		if(document.getElementById('cat_' + categories[i]).checked == true)
			cats = cats + '&cat_' +  categories[i] + '=1';
		else
			cats = cats + '&cat_' +  categories[i] + '=0';
	}

	var post_data = 'name=' + name + '&aperture=' + aperture + '&iso=' + iso + '&focal_length=' + focal_length + '&camera=' + camera + '&model=' + model + '&description='+ description + cats;
	document.getElementById('edit_pic_msg').innerHTML = 'Loading please wait...';
	new Ajax.Request('/photos/edit_pic/'+pic_id, {method: 'post', postBody: post_data, onSuccess: doLoadEditPic});

}

function loadSpecialPicPopup(picurl) {
	//Hide other popup types to avoid bugs;
	hideModelPopup();
	hideOtherPopup();
	hidePopup();
	populatePopup( '<div id=\"specialPicContent\"><img src=\"/images/preloader.gif\" /></div>' )
	showCenteredPopup(true);

	specailImg = new Image();
	specailImg.onload = loadSpecailPic;
	specailImg.src = picurl;
}

function loadSpecailPic()
{
	$('specialPicContent').innerHTML = '<img src="'+specailImg.src+'"  style="border:3px solid black" />';
	reCenterPopup();
}

function showUploadBar() {

	var pic = document.getElementById('file').value;
	if(pic == '')
	{
		fillPopup("<div class=\"flashmsg error\"><img src=\"/images/ico_error.png\" alt=\"\" align=\"absmiddle\" class=\"msgIco\" />&nbsp;Please select a file!</div>");
		showCenteredPopup();
		setTimeout('hidePopup()', 2500);
		return false;
	}

	hideModelPopup();
	hideOtherPopup();

	showDimmer(true);
	fillPopup("Please wait while your file is being uploaded!<br /><br /><img src=\""+getImage('upload')+"\" alt=\"Upload bar...\" width=\"220\" height=\"19\" />");
	showCenteredPopup();

	return true;
}

function showEventPopup(data) 
{
	hidePopup();
	if ( typeof data == 'object' ) 
	{
		setOnScreen();
		modelProfileVisible = true;
		
		populatePopup('<table class="modelInfo"><tr><td class="capitalise"><img src="'+data.img+'" width="'+data.width+'" height="'+data.height+'"/></td></tr></table>', true);
	}
}

var myAnim2;

function initHS() {
	//Functions
	this.prepare 	= prepareHS;
	this.show		= showHS;
	this.hide		= hideHS;
	this.setText	= setTextHS;
	//Parameters
	this.text		= '';

	this.predefines	= predefines;
	this.holder = document.createElement('div');
	var table 	= document.createElement('table');
	var tbody 	= document.createElement('tbody');
	var tr		= document.createElement('tr');

	this.holder.style.visibility	= 'hidden';
	this.holder.id					= 'helper';
	this.holder.style.position 		= "absolute";
	//this.holder.style.height 		= '200px';
	this.holder.style.width 		= '460px';
	this.holder.style.zIndex 		= 120;
	this.holder.style.left			= '550px';
	this.holder.style.top			= 0;
	this.holder.style.overflow		= 'hidden';

	table.width	= '100%';
	table.border	= '0';
	table.style.borderCollapse	= 'collapse';
	table.style.position 		= "absolute";
	table.style.bottom	= 0;

	//Create the second row...
	var tr = document.createElement('tr');

	var td1 = document.createElement('td');
	td1.style.padding 		= '0';
	td1.style.background 	= 'url('+WEBPATH+'images/frame/frame_bgnd_left.png)';
	td1.style.behavior	 	= 'url('+WEBPATH+'js/iepngfix.htc)';
	td1.style.width	 		= '20px';
	td1.innerHTML 			= '&nbsp;';


	this.td_ico = document.createElement('td');
	this.td_ico.width 		= '20';
	this.td_ico.style.padding 		= '10px 6px 0 0';
	this.td_ico.style.background 	= 'url('+WEBPATH+'images/frame/frame_bgnd.png)';
	this.td_ico.style.behavior	 	= 'url('+WEBPATH+'js/iepngfix.htc)';
	this.td_ico.innerHTML 			= '<img src="'+WEBPATH+'images/ip_icon_big.png" class="msgIco">';
	this.td_ico.vAlign			= "top";
	///this.td_ico.style.textAlign		= "justify";


	this.td_c = document.createElement('td');
	this.td_c.style.padding 		= '13px 0 4px 4px';
	this.td_c.style.background 	= 'url('+WEBPATH+'images/frame/frame_bgnd.png)';
	this.td_c.style.behavior	 	= 'url('+WEBPATH+'js/iepngfix.htc)';
	this.td_c.innerHTML 			= '';
	this.td_c.style.color			= "white";
	this.td_c.style.textAlign		= "justify";

	var td3 = document.createElement('td');
	td3.style.padding 		= '0';
	td3.style.background 	= 'url('+WEBPATH+'images/frame/frame_bgnd_right.png)';
	td3.style.behavior	 	= 'url('+WEBPATH+'js/iepngfix.htc)';
	td3.innerHTML 			= '&nbsp;';
	td3.style.width	 		= '20px';

	tr.appendChild(td1);
	tr.appendChild(this.td_ico);
	tr.appendChild(this.td_c);
	tr.appendChild(td3);

	tbody.appendChild(tr);

	//Create the last row...
	var tr = document.createElement('tr');

	var td1 = document.createElement('td');
	td1.style.padding 				= '0';
	td1.style.backgroundImage		= 'url('+WEBPATH+'images/frame/frame_corners.png)';
	td1.style.backgroundPosition 	= '0 -20px';
	if (isIE) td1.style.behavior	= 'url('+WEBPATH+'js/iepngfix.htc)';


	var td2 = document.createElement('td');
	td2.colSpan 					= 2;
	td2.style.padding 				= '0';
	td2.style.background 			= 'url('+WEBPATH+'images/frame/frame_bgnd_bottom.png)';
	if (isIE) td2.style.behavior	= 'url('+WEBPATH+'js/iepngfix.htc)';
	td2.height 						= '20';
	td2.innerHTML 					= '&nbsp;';

	var td3 = document.createElement('td');
	td3.style.padding 				= '0';
	td3.style.backgroundImage		= 'url('+WEBPATH+'images/frame/frame_corners.png)';
	td3.style.backgroundPosition 	= '-20px -20px';
	if (isIE) td3.style.behavior	= 'url('+WEBPATH+'js/iepngfix.htc)';
	td3.innerHTML 					= '&nbsp;';

	tr.appendChild(td1);
	tr.appendChild(td2);
	tr.appendChild(td3);

	tbody.appendChild(tr);

	table.appendChild(tbody);
	this.holder.appendChild(table);
	document.body.appendChild(this.holder);
}

function prepareHS(id) {
	this.text = this.predefines[id];
}

function setTextHS(text) {
	this.text = text;
}

function showHS(id) {
	//if (!pageLoaded) return;

	if (typeof id != 'undefined') {
		this.prepare(id);
	}
	this.td_c.innerHTML 		 = this.text;
	if (parseInt(posx) > 550) {
		this.holder.style.left		 = '50px';
	}else{
		this.holder.style.left		 = '550px';
	}
	//this.holder.style.left		 = '550px';
	this.holder.style.top		 = windowScrollY + 'px';
	this.holder.style.visibility = 'visible';

	var myAnim2 = new YAHOO.util.Anim('helper', {height: {from:0, to: parseInt(this.td_c.clientHeight)+20}}, 1, YAHOO.util.Easing.bounceOut);
	myAnim2.animate();
	//alert(myAnim2.animate());
}

function hideHS() {
	//if (!pageLoaded) return;

	var myAnim = new YAHOO.util.Anim('helper', {height: {to: 0}}, 1);
	myAnim.animate();
	//myAnim.onComplete.subscribe(removeElement);
	//this.holder.style.visibility = 'hidden';
}
