$(function(){
	$('#search_form button').button({icons:{primary: 'ui-icon-circle-triangle-e'},text:false});
	$('#search_form button').click(function(){
		return ($('#q').val().length!=0 && $('#q').val() != 'поиск...');										
	});
	
	$('#q').focus(function(){
		$(this).css('color','#000');
		$(this).css('border-color', '#666');
		$(this).val('');
	});
	
	$('#q').blur(function(){
		var val = ($(this).val().length == 0) ? 'поиск...' : $(this).val();
		$(this).val(val);
		$(this).css('border-color', '#ccc');		
		$(this).css('color', '#999');
	});
	
	$('#btnFeedback').button({icons:{primary: 'ui-icon-mail-open'}});
	$("#dlgFeedback").dialog({modal:true,autoOpen:false,closeOnEscape:true});
    //,buttons:{
 //				'Отправить': function() {
					//TODO: call web service submit
//					$(this).dialog('close');
 //					$(this).dialog('submit');
//				}
//			}});
	$('#btnFeedback').click(function() {
		$('#dlgFeedback').dialog('open');
		return false;
	});
	
	
	$('#slider-pane').html( $('#slider-pane').html() + '<div id="slider-wrapper"><button id="slider-left">пролистать влево</button><div id="slider"></div><button id="slider-right">пролистать вправо</button></div>' );
	var cnt = $('#slider-content').find('table:first');
	var maxVal = (cnt.width() - $('#slider-content').width());
	$("#slider").slider({
		min: 0,
		max: maxVal,
		stop: function (ev, ui) {
			cnt_anim(ui.value);
		}
	}); 
	
	$('#slider-right').button({icons:{primary: 'ui-icon-carat-1-e'},text:false});
	$('#slider-left').button({icons:{primary: 'ui-icon-carat-1-w'},text:false});
	
	$('#slider-right').click(function(){
		var curVal = $('#slider').slider('value');
		newVal = curVal + maxVal/10;
		if(newVal > maxVal) newVal = maxVal;		
		$("#slider").slider( "value" , newVal );
		cnt_anim(newVal);
	});
	
	$('#slider-left').click(function(){
		var curVal = $('#slider').slider('value');
		newVal = curVal - maxVal/10;
		if(newVal < 0) newVal = 0;		
		$("#slider").slider( "value" , newVal );
		cnt_anim(newVal);
	});
	
	function cnt_anim(val) {
		cnt.animate({ 'left' : '-' + val + 'px' }, 500, 'linear');
	}
	
	if($.browser.msie && $.browser.version == '7.0') {
		$('#slider-left').css('top','-3px');
		$('#slider-right').css('top','-3px');
	}
	
	$('.btnAddToCart').button({icons:{primary: 'ui-icon-cart'}});
	
	if(document.getElementById('new_products')) {
		var maxHeight = 0;
		$('#new_products > .item > .product_card').each(function(){
			var height = $(this).height();
			if(height > maxHeight) maxHeight = height;
		});
		
		$('#new_products > .item > .product_card').each(function(){
			$(this).css('height', maxHeight);
			$(this).parent().css('height', maxHeight+2);
		});
		
		$('<div class="clr">&nbsp;</div>').insertAfter('#new_products > .item:odd');
	}
	
	if(document.getElementById('news')) {
		$("#news").accordion();
	}
	
	$('<div id="dlgLightBox" style="display:none"></div>').insertAfter('#container');
	$("#dlgLightBox").dialog({width:'auto',resizable:false,modal:true,autoOpen:false,closeOnEscape:true});
	
	$('.lnkBigImage').click(function(){
		var imgSrc = $(this).attr('href');
		
		var title = $(this).closest('table').find('h4 > a').html();
		$("#dlgLightBox").dialog('option', 'title', title);
		$("#dlgLightBox").html('<div style="text-align:center;padding:50px;"><img src="images/loader.gif" alt="загрузка..." /></div>');		
		$("#dlgLightBox").dialog('open');
		
		var img = new Image();
		$(img).load(function () {			
			$(this).hide();
			$("#dlgLightBox").html('');
			$("#dlgLightBox").append(this);    
			$(this).fadeIn();
			
			var center = parseInt($(document.body).width()/2);
			var curWidth = parseInt($("#dlgLightBox").dialog('widget').width());
			var left = parseInt(center - curWidth/2);
			var top = parseInt($(document).scrollTop() + 20);
			$("#dlgLightBox").dialog('widget').css('left', left+'px');
			$("#dlgLightBox").dialog('widget').css('top', top+'px');
		}).attr('src', imgSrc);
		return false;
	});
});


function btnAddToCart_Click(btn,opts) {
	//TODO: call web service
	$(btn).parent().html('<button style="color:#999;background:#f2f2f2;cursor:default;" class="op ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon" disabled="disabled"><span class="ui-button-icon-primary ui-icon ui-icon-check"></span><span class="ui-button-text">В корзине</span></button>');
	return false; //prevent postback;
}

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

var newDiv;
var parentDiv;
var parent;
var subMenuName;

function jsddm_open() {
	jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').css('visibility', 'visible');	
}

function jsddm_close() {
	if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer() {
	closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer() {
	if(closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

document.onclick = jsddm_close;

$(function(){
	$('#main_menu > li').bind('mouseover', jsddm_open);
	$('#main_menu > li').bind('mouseout',  jsddm_timer);
});
