function trim(s)
{
  return rtrim(ltrim(s));
}

function ltrim(s)
{
  return s.replace(/^\s+/, ''); 
}

function rtrim(s)
{
  return s.replace(/\s+$/, ''); 
}

$(document).ready(function()
	{
		var href = (document.location.href).split("/");
		$("a.zoom").fancybox({
				'zoomSpeedIn'		:	500,
				'zoomSpeedOut'		:	500
		});
		
		if(href[href.length -2] == 'become') {
			$(".form_list").html("<img src='/images/spinner.gif'>");
			$.ajax({
					type: "POST",
					url: "/modelscv/83/",
					cache: false,
					async: true,
					dataType: "html",
					data: "ajax=true",
					success: function(msg) {
						$(".form_list").html(msg);
						if($("#sex0").attr("checked") == true) {
							$("#breast").parent().parent().hide();
							$("#waist").parent().parent().hide();
							$("#hip").parent().parent().hide();
							$("#size").parent().parent().show();
						}
						
						if($("#sex1").attr("checked") == true) {
							$("#breast").parent().parent().show();
							$("#waist").parent().parent().show();
							$("#hip").parent().parent().show();
							$("#size").parent().parent().hide();
						}
					}
			});
			
			$( "#date_birth" ).live("click", function() {
				$(this).datepicker({"dateFormat": "yy-mm-dd",
									"defaultDate": "-18y",
									"changeYear": "true",
									"changeMonth": "true"});
				$(this).datepicker("show");
			});
			
			$("#sex0, #sex1").live("click", function() {
				if($("#sex0").attr("checked") == true) {
					$("#breast").parent().parent().hide();
					$("#waist").parent().parent().hide();
					$("#hip").parent().parent().hide();
					$("#size").parent().parent().show();
				}
			
				if($("#sex1").attr("checked") == true) {
					$("#breast").parent().parent().show();
					$("#waist").parent().parent().show();
					$("#hip").parent().parent().show();
					$("#size").parent().parent().hide();
				}	
			});
			
			$("#becomemodel").live("submit", function() {
				var flag_mail = flag_lname = flag_fname = flag_dbirth = flag_place = flag_phone = flag_code = 1;
				//Email
				var entered_value = $("#email").val();
				entered_value = entered_value.replace("/^\S+/iu","");
				re = /^([A-z0-9_\-]+\.)*[A-z0-9_\-]+@([A-z0-9][A-z0-9\-]*[A-z0-9]\.)+[A-z]{2,4}$/i;
				if(re.test(entered_value)) {
					$("#email").css("background", "");
				}
				else {
					flag_mail = 0;
					$("#email").css("background", "red");
				}	
				
				var re = /\S+/;
				
				//Name
				if(!$("#first_name").val().match(re)) { 
					flag_fname = 0;
					$("#first_name").css("background", "red");
				}
				else {
					$("#first_name").css("background", "");
				}	
				
				if(!$("#last_name").val().match(re)) { 
					flag_fname = 0;
					$("#last_name").css("background", "red");
				}
				else {
					$("#last_name").css("background", "");
				}
				//Birth
				if(!$("#date_birth").val().match(re)) { 
					flag_dbirth = 0;
					$("#date_birth").css("background", "red");
				}
				else {
					$("#date_birth").css("background", "");
				}

				//Phone		
				if(!$("#phone").val().match(re)) { 
					flag_phone = 0;
					$("#phone").css("background", "red");
				}	
				else {
					$("#phone").css("background", "");
				}
				
				//Place
				if(!$("#place").val().match(re)) { 
					flag_place = 0;
					$("#place").css("background", "red");
				}	
				else {
					$("#place").css("background", "");
				}
				
				//Code
				if($("#scode").val() !== $("#self").val()) { 
					flag_code = 0;
					$("#self").css("background", "red");
				}	
				else {
					$("#self").css("background", "");
				}
				
				if(flag_mail && flag_lname && flag_fname && flag_dbirth && flag_place && flag_phone && flag_code) {
					return true;
				}
				return false;
				
			});
		}
		
		//Студия
		if(href[href.length -2] == 'studio') {
			$( "div.photo_list").html("<img src='/images/spinner.gif'>");
			$("#listS").html("<img src='/images/spinner.gif'>");
			
			$.ajax({
					type: "POST",
					url: "/fotostudio/84/authors",
					cache: false,
					async: true,
					dataType: "html",
					data: "ajax=true",
					success: function(msg) {
						$( "div.photo_list").html(msg);
						$("a.zoom").fancybox({
							'zoomSpeedIn'		:	500,
							'zoomSpeedOut'		:	500
						});
					}
			});
			
			$.ajax({
					type: "POST",
					url: "/fotostudio/84/works",
					cache: false,
					async: true,
					dataType: "html",
					data: "ajax=true",
					success: function(msg) {
						$("#listS").html(msg);
						var n= ($("#listS").width())/140;
						n = n - (n%1);

						$("#listS").jCarouselLite({
							btnNext: ".next",
							btnPrev: ".prev",
							mouseWheel: true,
							visible: n
						});	
						
						$("a.zoom").fancybox({
							'zoomSpeedIn'		:	500,
							'zoomSpeedOut'		:	500
						});
					}
			});
		}
		
		if(href[href.length -2] == 'admin') {
			$( ".date_birth" ).live("click", function() {
				$(this).datepicker({"dateFormat": "yy-mm-dd",
									"defaultDate": "-18y",
									"changeYear": "true",
									"changeMonth": "true"});
				$(this).datepicker("show");
			});				
			$( ".date_casting" ).live("click", function() {
				$(this).datepicker({"dateFormat": "yy-mm-dd",
									"defaultDate": "+1w",
									"changeMonth": "true"});
				$(this).datepicker("show");
			});
		}

		
		/*Нл главной страничке галерею делаем*/
		if(href.length == 4) {
			$("#portfolio").html("<img src='/images/spinner.gif'>");
			$.ajax({
					type: "POST",
					url: "/photogalleries/22/mainpage",
					cache: false,
					async: true,
					dataType: "html",
					data: "ajax=true",
					success: function(msg) {
						$("#portfolio").html(msg);
						$('#portfolio ul').innerfade({
							speed: 1000,
							timeout: 5000,
							type: 'sequence',
							containerheight: '220px'
						});
					}
			}); 
		}
		
		if(href[href.length -2] !== 'become') {
			$("#status, #place, #age, #height, #size, #eyes, #hair, #shoes, #breast, #waist, #hips").live("change", function() {
				var url = "/" + href[3] + "/";
				/*for(i = 3; i < href.length; i++) {
					if(href[i]) {
						url += href[i];
						url += "/";
					}	
				}*/
				$.ajax({
							type: "POST",
							url: url + "80/search",
							cache: false,
							async: true,
							dataType: "html",
							data: "ajax=true&" + $("#filter_models").serialize(),
							success: function(msg) {
								$(".girl_list").replaceWith(msg);
								checkCart();
							}
				});
				
			});
		}
			
		//Кастинг
		$(".niceCheck").live("click", function() {
			var id = $(this).children("input").attr("id");
			id = id.split("_");
			if($(this).children("input").attr("checked")) {
				$("#casting_small").html("<img src='/images/spinner.gif'>");
				var cart = orderItem(id[1], 1, '', '');
				$("#casting_small").replaceWith(cart[1]);
				//animateItem(id[1]);
			}
			else {
				$("#casting_small").html("<img src='/images/spinner.gif'>");
				var cart = deleteItem(id[1], 1, '');
				$("#casting_small").replaceWith(cart[1]);
				
			}
		});
		
		checkCart();
	
		$(".deleteitems").live("click", function() {
			var id = $(this).attr("id");
			id = id.split("_");
			var cart = deleteItem(id[1], 1, '');
			$("#model_" + id[1]).attr("checked", false);
			$("#model_" + id[1]).parent().css("background-position","0 0");	
			$("#casting_small").replaceWith(cart[1]);
		});
		
		$(".addcasting").live("click", function() {
			var id = $(this).attr("id");
			id = id.split("_");
			var str = $("#mycastnum").text();
			var num = 0;
			if(str) {
				str = str.replace("(", "").replace(")", "");
				var num = parseInt(str);
			}
			
			num += 1;
			orderItem(id[1], 1, '', '');
			$("#mycastnum").text("(" + num + ")");
			$(this).replaceWith("<a href=\"javascript:;\" id=\"removecasting_" + id[1] + "\" class=\"removecasting\">Участвует в вашем кастинге (-)</a>");
		});
		
		$(".removecasting").live("click", function() {
			var id = $(this).attr("id");
			id = id.split("_");
			var str = $("#mycastnum").text();
			var num = 0;
			if(str) {
				str = str.replace("(", "").replace(")", "")
				var num = parseInt(str);
			}
			num -= 1;			
			deleteItem(id[1], 1, '', '');
			if(num <= 0) {
				$("#mycastnum").text("");
			}
			else {
				$("#mycastnum").text("(" + num + ")");
			}
			$(this).replaceWith("<a href=\"javascript:;\" id=\"addcasting_" + id[1] + "\" class=\"addcasting\">Добавить в кастинг (+)</a>");
		});
		
		
		$("#shop_send").live("click", function() {
			var flag_mail = flag_name = flag_phone = 1;
			//Email
			var entered_value = $("#shop_mail").val();
			entered_value = entered_value.replace("/^\S+/iu","");
			re = /^([A-z0-9_\-]+\.)*[A-z0-9_\-]+@([A-z0-9][A-z0-9\-]*[A-z0-9]\.)+[A-z]{2,4}$/i;
			if(re.test(entered_value)) {
				$("#shop_mail").css("background", "");
			}
			else {
				flag_mail = 0;
				$("#shop_mail").css("background", "red");
			}	
			
			var re = /\S+/;
			
			//Name
			if(!$("#shop_name").val().match(re)) { 
				flag_name = 0;
				$("#shop_name").css("background", "red");
			}
			else {
				$("#shop_name").css("background", "");
			}	

		//Phone		
			if(!$("#shop_phone").val().match(re)) { 
				flag_phone = 0;
				$("#shop_phone").css("background", "red");
			}	
			else {
				$("#shop_phone").css("background", "");
			}	
			
			if(flag_mail && flag_name && flag_phone) {
				var form = $(this).parent("form");
				$.ajax({
						type: "POST",
						url: "/cart/30/order",
						cache: false,
						async: false,
						dataType: "json",
						data: "ajax=true&" + form.serialize(),
						success: function(msg) {
							if(msg[0] == 1) {
								$(".block_order").html("<div style='text-align:center;'><p></p><p>Спасибо за ваш заказ</p></div>");
								$(".block_order").fadeOut(2100, function() {
									$('#fade, a.close').remove(); 
									$(".card").remove();
									$("h1").after('<p class="error">Ваша корзина пуста</p>');									
								});
							}
						}
				});
			}
		});
		
		
		function checkCart() {
			var cart = getSmallCart();

			$(".niceCheck").each(function() {
				var id = $(this).children("input").attr("id");
				id = id.split("_");
				
				var flag = false;
				for(var i = 0; i < cart[2].length; i++) {
					if(cart[2][i] == id[1]) {
						flag = true;
						break;
					}
				}
				if(flag) {
					$("#model_" + id[1]).attr("checked", true);
					$("#model_" + id[1]).parent().css("background-position","0 -14px");	
				}
				else {
					$("#model_" + id[1]).attr("checked", false);
					$("#model_" + id[1]).parent().css("background-position","0 0");	
				}
			});
		}
		
		function setCart(id, num) {
			$.ajax({
					type: "POST",
					url: "/cart/30/setcart",
					cache: false,
					async: true,
					dataType: "json",
					data: "ajax=true&item_id=" + id + "&number=" + num
			}); 	
		}
		
		
		function countCart() {
			var ret = 0;
			$.ajax({
					type: "POST",
					url: "/cart/30/countcart",
					cache: false,
					async: false,
					dataType: "json",
					data: "ajax=true",
					success: 	function(msg){
									ret = msg[1];
								}
			}); 
			
			return ret;			
		}
		
		function inCart(id) {
			var ret = false;
			$.ajax({
					type: "POST",
					url: "/casting/82/incart",
					cache: false,
					async: false,
					dataType: "json",
					data: "ajax=true&item_id=" + id,
					success: 	function(msg){
									if(msg[0] == 1) {
										ret = true;
									}
								}
			}); 
			
			return ret;			
		}
		
		function animateItem(id) {
			$("#deletemodel_" + id).parent().children("span").animate({ backgroundColor: "white" }, 1300).animate({ backgroundColor: "#3d3d3d" }, 4000);
			return false;
		}
		
		function animateRemoveItem(id) {
			$("#cartcontainer_" + id).animate({ backgroundColor: "yellow" }, 500).animate({ opacity: "hide" }, 700);
			/*$("#cartcontainer_" + id).remove();*/
			return false;
		}
		
		function getSmallCart(){
			var cart = "";
			$.ajax({
					type: "POST",
					url: "/casting/82/showcart",
					cache: false,
					async: false,
					dataType: "json",
					data: "ajax=true",
					success: 	function(msg){
									cart = msg;
								}
			}); 
			
			return cart;
		}
		
		function orderItem(id, number, element, new_element){
			var cart = "";
			$.ajax({
					type: "POST",
					url: "/casting/82/addtocart",
					cache: false,
					data: "ajax=true&item_id="+id+"&number="+number,
					async: false,
					dataType: "json",
					success: 	function(msg){
									cart = msg;
								}
			}); 
			
			return cart;
		}

		function deleteItem(id, number, element) {
			var cart = "";
			$.ajax({
					type: "POST",
					url: "/casting/82/delete/" + id,
					cache: false,
					data: "ajax=true&number="+number,
					async: false,
					dataType: "json",
					success: 	function(msg){
									cart = msg;
								}
			});
			
			return cart;
		}
		
	}

);


function killBlock(bid)
{
   document.getElementById(bid).style.display='none';
}

function hideBlock(id) {
		document.getElementById(id).style.display='none';
}

function viewBlock(id) {
		document.getElementById(id).style.display='inline';
}

function submitTheForm(form)
{
  d = document.getElementById(form);
  d.submit();
}

function submitTheFormAction(form, action, close)
{
  d = document.getElementById(form);
  d.action = action;
  d.submit();
  if(close)
	{
	  setTimeout("closereload()", 200);
	}
}

function closereload()
{
  window.opener.location.reload();
  window.close();
}

function onloadPageFormsBlocks(menu_ids, shorts)
{
  menu_ids = menu_ids.split(",");
  shorts   = shorts.split(",");

  for(i=0;i<menu_ids.length;i++)
  {
	for(j=0; j<shorts.length; j++)
	  {
		d = document.getElementById('menuid'+shorts[j]+'_'+menu_ids[i]);
		if(d)
		  {
			d1 = document.getElementById('menufile_'+shorts[j]+'_'+menu_ids[i]);

			if(d.checked)
			  {
				d1.style.display = '';
			  }
			else
			  {
				d1.style.display = 'none';
			  }
		  }
	  }
  }
}

function chechInquiry(source)
{
  d = document.getElementById(source);
  d.checked = true;
}

function checkboxes(do_check, elem)
{
  var elts = document.forms['search'].elements[elem];
  if(elts[0])
	{
	  for(var i = 0; i < elts.length; i++)
		elts[i].checked = do_check;
	}
  else
	  elts.checked = do_check;
}

function checkstring(form, string, boxes)
{
  d  = document.getElementById(form);
  s  = document.getElementById(string);

  var cond1 = '';

  var elts = document.forms[form].elements[boxes];
  if(elts[0])
	{
	  for(var i = 0; i < elts.length; i++)
		{
		  if(elts[i].checked)
			cond1 = true;
		}
	}
  else
	{
	  if(elts.checked)
		cond1 = true;
	}

  if(!(s.value))
	alert("Вы не ввели строку для поиска");
  else if(!cond1)
	alert("Вы не выбрали направление поиска");
  else
	d.submit();
}

function showhide(what,what2)
{
  if (what.style.display=='none')
	{
	  what.style.display='';
	}
  else
	{
	  what.style.display='none'
	}
}













var myWidth=0;
function init_gallery() {
		setInterval('startLine()', 1);
}

function startLine() {
		if (document.body.clientWidth != myWidth) {
				myWidth = document.body.clientWidth;
				inLine();
		}
}
function inLine() {
		var td = document.getElementById('photo-parent').getElementsByTagName('td');
		var table = document.getElementById('photo-parent').getElementsByTagName('table');
		var count = 0;
		var row = false;

		for (var i = 1; (table.item(i)); i++) {
				if ((row == false) && (table.item(i).offsetTop == table.item(i-1).offsetTop)) count++; else row = true;
				td.item(i).style.height = 'auto';
		}
		td.item(0).style.height = 'auto';
		count++;

		for (var i = 0; (td.item(i)); i=i+count) {
				myHeight = 0;
				for (var o = 0; (td.item(i+o) && o < count); o++) if (td.item(i+o).offsetHeight > myHeight) myHeight = td.item(i+o).offsetHeight;
				for (var o = 0; (td.item(i+o) && o < count); o++) td.item(i+o).style.height = myHeight + 'px';
		}
		document.getElementById('photo-parent').style.visibility = 'visible';
}

function submitTheForm(form)
{
  d = document.getElementById(form);
  d.submit();
}

function submitTheFormAction(form, action, close)
{
  d = document.getElementById(form);
  d.action = action;
  d.submit();
  if(close)
	{
	  setTimeout("closereload()", 200);
	}
}

function closereload()
{
  window.opener.location.reload();
  window.close();
}

function onloadPageFormsBlocks(menu_ids, shorts)
{
  menu_ids = menu_ids.split(",");
  shorts   = shorts.split(",");

  for(i=0;i<menu_ids.length;i++)
  {
	for(j=0; j<shorts.length; j++)
	  {
		d = document.getElementById('menuid'+shorts[j]+'_'+menu_ids[i]);
		if(d)
		  {
			d1 = document.getElementById('menufile_'+shorts[j]+'_'+menu_ids[i]);

			if(d.checked)
			  {
				d1.style.display = '';
			  }
			else
			  {
				d1.style.display = 'none';
			  }
		  }
	  }
  }
}



function setMenuOver(elem) {
   elem.className = 'over';
}

function setMenuOut(elem) {
   elem.className = 'default';
}

