﻿/**
 * @author Leha Stratan (ls@tercio.ru)
 */


$(document).ready(main);

function main(){
	
	Box();
	PhotoChanger();	
	CurrentDate();
	Toggler();	
	Catalogue();	
	CleverTextField("#search input");
	CleverTextField("#ext-search .ext-search");	
	CurrentLinks();	

	$("table.zebra tr:odd").addClass("odd");
}


function Box(){
	$("#hmenu td.parent").hover(
		function(){
			$("#hmenu div.box").show();
		},
		function(){
			$("#hmenu div.box").hide();
		}
	);
}










function PhotoChanger(){
//	var path = "/images/calendars/";
//	$("#good-item a.photochanger").each(function(){
//		$(this).click(function(){
//			file = $(this).text() + ".jpg";
//			$("#good-item img:first").attr({src:path + file});
//			$("#color-name").html("Тип обложки: <b>" + $(this).text() + "</b>");
//			
//			$("#good-item #colors tr").removeClass("active");
//			$(this).parent().parent().addClass("active");
//			
//			return false;
//			
//		});
//	});
	
	$("#good-item .tobasket a").each(function(){
		$(this).click(function(){			
			$(this).next().toggle();
			
			return false;
		});
	});
	
	$("#good-item div.close").each(function(){
		$(this).click(function(){			
			$(this).parent().hide();
		});
	});
}


/**
 * текущая дата
 */
function CurrentDate(){
	today = new Date();
	months = ["Января", "Февраля", "Марта", "Апреля", "Мая", "Июня", "Июля", "Августа", 
				"Сентября", "Октября", "Ноября", "Декабря"];
				
	$("#date span").text(today.getDate() + " " + months[today.getMonth()]);
}


/**
 * тогглер
 */
function Toggler(){
	$(".toggler").click(function(){
		$(this).toggleClass("expanded");
		$(this).next().toggle();
	});
	$(".toggler").hover(
		function(){
			$(this).addClass("hover");
		},
		function(){
			$(this).removeClass("hover");
		}
	);
}



/**
 * меню каталога и другие фишки
 */
function Catalogue(){
	$("div#catalogue-menu a.parent").prepend("<span></span>");
	$("div#catalogue-menu a.parent").each(function(index, elem){
		$(this).find("span:first").click(function(){
			$(this).parent().parent().find("ul:first").toggle();
			$(this).parent().toggleClass("expanded");
			
			return false;
		});
	});
	
	
	//раскрываем все дерево относительно .current 
	$("div#catalogue-menu li.current").each(function(){		
			$(this).parents("ul").css({display:"block"});
			$(this).children("ul").css({display:"block"});
	});
	
	
	$("#catalogue-menu").children("ul").children("li").each(function(index){
		if ((index+1) % 7 == 0){
			$(this).addClass("separator");
		}
	});
	
	/**
	 * фиксим список товаров
	 */
	$("#goods-list div.item:gt(1)").css({borderTop:"none"});
	$("#goods-list div.item:odd").css({borderLeft:"none"});
	
	
	/**
	 * селект с ценами
	 */
	$("#prices").change(function(){
		p = $(this).val();
		$("#price-from").val(p.substr(0, p.indexOf("-")));
		$("#price-to").val(p.substr(p.indexOf("-") + 1, p.length - p.indexOf("-")));
	});
}




/**
 * Умное текстовое поле. При фокусе текст по умолчанию стирается,
 * при потере фокуса либо восстанавливается, либо остается тем, что 
 * ввел пользователь
 * 
 * @param {Object} strObj селектор для jQuery. Обрамляется кавычками.
 */
function CleverTextField(strObj){
	var cleverTextField = $(strObj);
	var defaultText = cleverTextField.val();
	cleverTextField.focus(function(){
		if ($(this).val() == defaultText ){
			$(this).val("");
		}
	});
	cleverTextField.blur(function(){
		if ($(this).val() == ""){
			$(this).val(defaultText);			
		}
	});
}


/**
 * TODO: сверстать футер без скрипта
 */
function Footer(){	
	c = 160;	 
	ar = new Array;
	ar = [$("#left").height(), $("#middle").height(), $("#right").height()];
	
	m = ar[0];
	for(i=0; i<ar.length; i++){
		if(ar[i] > m){
			m = ar[i]
		}
	}
	
	h =  document.compatMode=='CSS1Compat' && !window.opera ? 
			document.documentElement.clientHeight : document.body.clientHeight;

	d = m + c;
	
	if (d < h){
		r = d;
		$("body").removeClass("big")
	} else {
		r = h;
		$("body").addClass("big")
	}	
}




/**
 * даем текущим ссылкам класс current
 */
function CurrentLinks(){
	loc = document.location.pathname;
	$("#hmenu a").each(function(){
		if ($(this).attr("href") == loc) {
			$(this).addClass("current");
		}		
	});

}










