
// ** initial settings
var base_url = $("base").attr("href");

$(document).ready(function(){

// ** 	prev atlant
	$('ul#slidefoto').innerfade({
			speed: 1000,
			timeout: 5000,
			type: 'sequence',
			containerheight: '381px'
			});
	function searchValid(searchStr,len){
			regex=/^[a-zA-Zа-яА-ЯёЁ0-9 ]+$/;
			regexlen=/^[a-zA-Zа-яА-ЯёЁ0-9 ]{3,}$/;
			if (len) isValid = regexlen.test(searchStr) && (searchStr != ' Поиск'); 
				else isValid = regex.test(searchStr) && (searchStr != ' Поиск'); 
			if (!isValid) $('#searchMsg').fadeIn().click(function (){
				$('#searchMsg').fadeOut();
				return false;	
				});
			else $('#searchMsg').fadeOut();	
			//alert(isValid);
			return isValid;
		}
	$('form.search').submit(function (){
			return searchValid($('#searchField').val(),true);
		})
		.after('<div id="searchMsg"></div>');
	position = $('form.search').offset();
	$('#searchMsg').hide()
				.html('<div class="msgtop"></div><div class="msg">Строка поиска должна быть <br/> более трех символов и содержать<br/>  только буквенные символы</div>')
				.css({position: 'absolute', top: position.top+20, left:position.left-170});
	$('#searchField').focus(function(){ if ($(this).val() == ' Поиск') $(this).val('')})
		.keyup(function(){searchValid($('#searchField').val(),false);});


	
// ***		
// ** 	EOF prev atlant


	$(".gMenu a").each(function(){
		var td_el = $("table td", $(this));
		var a_el = $(this);
		$(td_el).click(function(){
			location.href = base_url+a_el.attr("href");
		});
	});


	fix_inline_links();
//	$('#dropdown').droppy();
//	alert('af');						   	

	//	***		***			***			***
	//	***		Search adds
	//	***		***			***			***
	$(".search_panel input").each(function(){
		var val = $(this).val();
//		alert('asf'+val);
		if (val=="")	$(this).val($(this).attr("title"));
		$(this).attr("lang", $(this).attr("name"));
	}).bind("focus",function(){
		var val = $(this).val();
		if (!empty(val) && val.toLowerCase()==$(this).attr("title")) $(this).val('');
	}).bind("blur",function(){
		var val = $(this).val();
		if (empty(val) || val=="") $(this).val($(this).attr("title"));
	});
	$(".search_panel_wide button[type='submit']").bind("click",function(){
		var el_feeder = $('.search_panel_wide #model');
		if ($(el_feeder).val()==$(el_feeder).attr('title'))	{
//			alert('a');
			$(el_feeder).focus();
			return false;
		}
//		if (empty(val) || val=="") $(this).val($(this).attr("title"));
	});
	


$('div.photo').each(function(){
	var title = $(this).attr("lang");
	if (!empty(title)) $(this).attr("title", title);
});

if (location.href) var href_curr = location.href;
if (!empty(href_curr)) {
	if (href_curr.indexOf("#accessories")>0) {
	}	else $("#acsr").hide();
}
//alert(location.href);
$('.acessories a').click(function(){
	$("#acsr").show();
});


// ** fix cross navigation
//$(".vMenu a.item_menu").each(function(){	var el_href = $(this).attr("href");	if (!empty(el_href)) $(this).attr("href", el_href+'&all');});




$('.instant_suggest').each(function(){
	var dt = $(this).attr("lang");
//	alert(dt);
	if (!empty(dt))
	$(this).instant_suggest('hr_gate.php?test=500&r='+Math.random(), 
	 	{	  disableHighlight:true
			, dataType:dt
			, sendConnected:true
			, minchars:1
			, onSelect: function() { // this - current LI entry
				return false;
			}
	});
	$(this).focus(function(){
//			setTimeout(function() { $(".ac_results").hide(); }, 1500);
	});
	
});


$('.suggest').each(function(){
	var dt = $(this).attr("lang");
//	alert(dt);
	if (!empty(dt))
	$(this).suggest('hr_gate.php?test=500&r='+Math.random(), 
	 	{	  disableHighlight:true
			, dataType:dt
			, minchars:1
			, onSelect: function() { // this - current LI entry
//				var go2link = $("a", this).attr("href");
//				var str = $(this).text();
//				alert("You selected: " + $(this).html());
//				$("#qs_cc").hide();
				$("#qs_sel").val($(this).attr('lang'));
//				alert('1'+dt);
/*				if (dt=="edu_res") {
//					alert('dbg'+ $("#qs_city_sel").val());
					
					if (!empty($('#id_tutor').val())) var controlled_el_selector = "select.student";
					if (!empty($('#id_student').val())) var controlled_el_selector = "select.tutor";
//					alert(controlled_el_selector);
												  
					if (!empty(controlled_el_selector))												  
					JsHttpRequest.query(
						'hr_gate.php?test=500&r='+Math.random(),
						{
							'sp': "edu.res_reg_persons"
							,'id_res': $("#qs_city_sel").val()
						},
						function(result, errors) {
							if (errors) alert(errors);
							if (result) {
//								alert(result.res)

								if (result.res=="ok" ) {
									//show_array(result.data.arr_region);
		
									var arr = result.arr;
//									arr_city = result.data.arr_city;
									$(controlled_el_selector).html('');
									for(var q in arr){
										$(controlled_el_selector).append("<option value=\""+q+"\">"+arr[q]+"</option>")
									}
									$(controlled_el_selector).focus();
								}
								//if (errors) alert(errors);
							} 
						},
						false  //disable caching
					);	

				}*/
				return false;
			}
	});
	$(this).focus(function(){
			setTimeout(function() { $(".ac_results").hide(); }, 500);
//			$(".ac_results").hide();
//			alert('1'+ hr_error);
	});
	
});
//alert('1');

/*
	$(".search_panel #model").bind("keyup",function(){
		// ** ajax
		var val = $(this).val();
		
	}); 
*/
	//	**		Price
	$(".search_panel #price_from, .search_panel #price_to").bind("keyup",function(){
		var val = parseFloat($(this).val());
		if (isNaN(val)) val = '';
		$(this).val(val);
	}); 
	/*
	$(".search_panel #price_to").bind("focus",function(){
		var val = $(this).val();
		if (!empty(val) && val.toLowerCase()=="цена до") $(this).val('');
	}).bind("blur",function(){
		var val = $(this).val();
		if (empty(val) || val=="") $(this).val('цена до');
	}).bind("keyup",function(){
		var val = parseFloat($(this).val());
		if (isNaN(val)) val = '';
		$(this).val(val);
	}); 
	*/
	

	$(".search_panel button[type=reset]").bind("click", function(){
		$(".search_panel input").each(function(){
			$(this).val($(this).attr("title"));
		});
		return false;
	}); 
/*
	$("#btn_login").bind("click", function(){
		$("#search_box").css("display", "none");
		$("#login_box").css("display", "block");
		$("#btn_search").removeClass().addClass("btn_search");
		$("#btn_login").removeClass().addClass("btn_login_active");
//		alert($("#btn_login").css("display"));
		return false;
	}); 

*/
	//	***		Homepage + Favorites
	$(".set_homepage").bind("click", function(){
		var url = location.href;											  
		if (!empty(url)) {
			$.setHomepage(url);
/*			document.body.style.behavior='url(#default#homepage)';
			if (!is_undef(window.external)) {
				window.external.setHomePage(url);
			} else	document.body.setHomePage(url);*/
		}
		return false;
	}); 
		
	//	***		Login adds
	

	// *** init infoBox
	/*
	var tmp_str = $("div.infoBox .top .content_title").text();//.trim()
	if (empty(tmp_str)) {
//	alert(tmp_str);
		for(var i=1; i <4; i++) {
			var tmp_head = $("div.infoBox .c .content_text h" + i + ":first");
//			alert($(tmp_head).html())
			var tmp_head_val = $(tmp_head).html();
			if (!empty(tmp_head_val)) {
				$("div.infoBox .top .content_title").html("<h1>"+tmp_head_val+"</h1>");				
				$(tmp_head).remove();
				break;
			}
		}
	}
*/

/*
	$('a.compare_zakaz, a.ma_link, a.ma_basket_link, a.cat_prod').bind('click',function(){//, a.ma_link
		var id = parseInt($(this).attr('id'));
		if (id > 0) {
			$(this).removeAttr('href');
			//добавляем в корзину товар с помощью ajax
			//'hr_gate.php?test=500&r='+Math.random(),
			//'../ims/logic/hr_prod_in_basket.php?test=500&r='+Math.random(),
			JsHttpRequest.query(
				'hr_gate.php?test=500&r='+Math.random(),
				{
					'sp': "prod_in_basket"
					,'add_id': $(this).attr('id')  
				},
				function(result, errors) {
					if (errors) alert(errors);
					if (result) {
						//alert("fg")
						if (result.count_prod && result.total_sum) update_count_basket(result.count_prod,result.total_sum);
					}
				},
				true  //disable caching
			);
			$(this).html('<a class="a_prod_in_cart" href="?p=ordering">Товар<br>в корзине</a>');
			$(this).attr('title','Перейти к корзине');
		}
	});
	*/				
	
	// ** dropdown brands on cat click
	/*
	$('a.item_menu').bind('click',function(e) {
		// ** cancel default
//		if (e.preventDefault)	e.preventDefault();	if (e.stopPropagation)	e.stopPropagation();	e.cancelBubble = true;	e.returnValue = false;
	
		var parent = $(this).parent();
//		alert($(parent).html());
		if (!empty(parent)) {
			$('a.item_submenu').each(function(){
				$(this).css('display', "none");
			});
			$('a.item_submenu', parent).each(function(){
//				$(this).toggle();
				var disp = $(this).css('display');
//				window.status = disp + $(this).attr("lang");
				var status = $(this).attr("lang");
//				alert(disp);
				if (status=="opened") {
					$(this).css('display', "none");
					$(this).attr("lang", "closed")
				} else {
					$(this).css('display', "block");
					$(this).attr("lang", "opened")
				}
			});
		}
		return false;	
	});
	*/

	$("a.toolbar-grid").bind("click", function(){
		$("div.goodsBox").hide().removeClass("goodsBox").addClass("goodsBoxGrid").show();
		$(".prodTbl div.descr").hide();
		return false;
	}); 

	$("a.toolbar-list").bind("click", function(){
		$("div.goodsBoxGrid").hide().removeClass("goodsBoxGrid").addClass("goodsBox").show();
		$(".prodTbl div.descr").show();
		/*$(".prodTbl div.iPic").css({
			float:'left'
//			, left:position.left-170
		});
		$(".prodTbl div.price_box").css({
			position: 'relative'
//			, top: position.top+20
//			, left:position.left-170
		});
		*/
		
		return false;
	}); 


	
	//	**		Dyn Add-Remove button handling
	$('.extend_controls button.add').bind('click', function(){
	//		$("#qs_datatype_sel").val($(this).attr("lang"));
			var parent = $(this).parent();
	//		var tmp_id = $(this).attr("id");
	//		alert($(this).attr("id")+ "_box"  + school_btn );
	
			var box2add = $(".add_box", parent).html();			
	//		var box2add = $(".add_box", this).html();			
	//		alert(box2add);
			if (!empty(box2add)) {
				$(this).before(box2add);
	//			alert($(parent).attr("lang"));
			}
			
	//	***							Multi-value boxes		***
			
			
	});		
	
	$('.extend_controls button.remove').bind('click', function(){
		if (confirm('Дійсно видалити?')) {
			var id_base = $(this).attr("id");
			if (!empty(id_base)) {
				id_base = id_base.substr(4);
			}
	//		alert(id_base);return;
	//		var tmp_id = $(this).attr("id");
			if (!empty(id_base)) {
				$("#city_"+id_base).remove();
				$("#"+id_base).remove();
				$("#btn_"+id_base).remove();
			}
		}
	});		


});


jQuery.extend({
setHomepage: function(url) {
if (document.all) {
 document.body.style.behavior = 'url(#default#homepage)';
 document.body.setHomePage(url);
}
else if (window.sidebar) {
 if (window.netscape) {
  try {
   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  }
  catch (e) {
   var strTemp = '';
   strTemp += "this action was aviod by your browser,";
   strTemp += "if you want to enable,please enter about:config in your address line,";
   strTemp += "and change the value of signed.applets.codebase_principal_support to true";
   alert(strTemp);
  }
 }
 var prefs = Components.classes['@mozilla.org/preferences-service;1']
     .getService(Components.interfaces.nsIPrefBranch);
 prefs.setCharPref('browser.startup.homepage', url);
}
}
});
