(function($) {

	$.fn.customFadeIn = function(speed, callback) {

		$(this).fadeIn(speed, function() {

    		//$('#side').css('z-index', '-1');

    		//$('#sort').css('z-index', '-1');

			if(jQuery.browser.msie)

				$(this).get(0).style.removeAttribute('filter');

			if(callback != undefined)

				callback();

		});

	};

	$.fn.customFadeOut = function(speed, callback) {

		$(this).fadeOut(speed, function() {

			if(jQuery.browser.msie)

				$(this).get(0).style.removeAttribute('filter');

			if(callback != undefined)

				callback();

    		//$('#side').css('z-index', '1000');

    		//$('#sort').css('z-index', '1');

		});

	};

})(jQuery);





jQuery.fn.center = function () {

    this.css("position","absolute");

    var ctop = (($(window).height() - this.outerHeight()) / 2) + $(window).scrollTop();

    if (ctop < $(window).scrollTop()+50) ctop = $(window).scrollTop()+50;

    this.css("top", ctop + "px");

    this.css("left", (($(window).width() - this.outerWidth()) / 2) + $(window).scrollLeft() + "px");

    return this;

}



$(document).ready(function() {



	$("#sort .mini .sortbutton a").click(function(){

		$("#sort .mini").hide();

		$("#sort .max").show();

		return false;

	});

	$("#sort .max .sortbutton a").click(function(){

		$("#sort .max").hide();

		$("#sort .mini").show();

		return false;

	});

	

	$('.show_full').click(function(){

		var url = $(this).attr('rel');

		url = url + '?template=82'

		show_full(url);

		return false;

	});

  

	$('#shadow').click(function(){

  		$('#shadow').customFadeOut(500);

  		$('#popup').fadeOut(500);

	});

	

	$('a.to_cart').click(function(){

		var form_name = 'to_cart_' + $(this).attr('rel');

		document.forms[form_name].submit();

		return false;

	});

	

	/*

	$('form.mini_search select').change(function(){

		document.forms['mini_search'].submit();

	});

	

	$('form.mini_search input').change(function(){

		document.forms['mini_search'].submit();

	});

	

	$('form.max_search select').change(function(){

		document.forms['max_search'].submit();

	});

	

	$('form.max_search input').change(function(){

		document.forms['max_search'].submit();

	});

	*/

	

	$('a.show_addq').click(function(){

		show_addq();

		return false;

	});



	$('.showBigImage').click(function(){

		showLoader();

		var link = $(this).attr('href');

  	$('#popup').html('<div style="text-align:center; height:564px"><img height="562" onClick="popupClose();" src="'+link+'" /></div>');

	

		popupSmall();

		$('#popup').center();

  	//$('#shadow').customFadeIn(500);

  	//$('#popup').fadeIn(500);

		return false;

	});

	

	$('#topmenu a').click(function(){

		var url = $(this).attr('href');

		if( url.substr(0, 9) != '/Netshop/') {

			showLoader();

			url = url + '?template=82'

			$('#popup').load(url, function() {

		  	//$('#shadow').customFadeIn(800);

		  	popupBig();

				$('#popup').center();

  			$('#popup').css({top: '100px'});

		  	//$('#popup').fadeIn(300);

		  	//$("#popup").slideto();

		  	//$('body').slideto({highlight: false});

		  	//$('#popup').center();

			});

			return false;

		}

	});

	

	

	

	

  //$('#topmenu tr td').bind('mouseover', jsddm_open);

  //$('#topmenu tr td').bind('mouseout',  jsddm_timer);

    

	$('#f_region').change(function(){

		changeRegion();

	});

	changeRegion();

    

	$('#f_delivery').change(function(){

		changeDelivery();

	});

	changeDelivery();

	

	

	$('img.captify').captify({

		// all of these options are... optional

		// ---

		// speed of the mouseover effect

		speedOver: 'fast',

		// speed of the mouseout effect

		speedOut: 'normal',

		// how long to delay the hiding of the caption after mouseout (ms)

		hideDelay: 500,	

		// 'fade', 'slide', 'always-on'

		animation: 'slide',		

		// text/html to be placed at the beginning of every caption

		prefix: '',		

		// opacity of the caption on mouse over

		opacity: '0.7',					

		// the name of the CSS class to apply to the caption box

		className: 'caption-bottom',	

		// position of the caption (top or bottom)

		position: 'bottom',

		// caption span % of the image

		spanWidth: '100%'

	});

	

	

});





function changeDelivery() {

	var delivery = $('#f_delivery').val();

	var deliveryText = $('#f_delivery option:selected').attr('cost');

	deliveryText = deliveryText ? deliveryText : 'Бесплатно';

	$('#deliveryText').text(deliveryText);

}



function changeRegion() {

		var region = $('#f_region').val();

		// reset delivery method

		$('#f_delivery').val(0);

		$('#f_DeliveryCost').val(0);

		// reset delivery text

		$('#deliveryText').text('Бесплатно');

		

		// delete all options

		$('#f_delivery option[value!=0]').remove();

			

		if(region==1) {

			// moscow

			// hide region name and city

			$('#inRegions').hide(300);

			$('#delMethod').show(300);

			// add options

			$('#f_delivery').append(del1);

		} else if (region==2) {

			$('#delMethod').show(300);

			$('#inRegions').show(300);

			// add options

			$('#f_delivery').append(del2);

		} else {

			$('#inRegions').hide(300);

			$('#delMethod').hide(300);

		}

}





/*

var timeout    = 500;

var closetimer = 0;

var ddmenuitem = 0;

 

function jsddm_open() {

    jsddm_canceltimer();

    jsddm_close();

    $('#side').css('z-index', '-1');

    $('#sort').css('z-index', '-1');

    ddmenuitem = $(this).find('ul').css('visibility', 'visible');

}

 

function jsddm_close() {

    if (ddmenuitem) ddmenuitem.css('visibility', 'hidden');

    if($('#shadow').css('display')=='none') {

    	$('#side').css('z-index', '1000');

    	$('#sort').css('z-index', '1');

    }

}

 

function jsddm_timer() {

    closetimer = window.setTimeout(jsddm_close, timeout);

}

 

function jsddm_canceltimer() {

    if (closetimer) {

        window.clearTimeout(closetimer);

        closetimer = null;

    }

}



document.onclick = jsddm_close;

*/



function showLoader() {

	$('#popup').html('<div style="width:100%; text-align:center; padding-top:50px; padding-bottom:50px;"><img src="/images/ajax-loader-3.gif" /></div>');

	$('#popup').center();

	$('#shadow').customFadeIn(300);

	$('#popup').fadeIn(300);

}



function popupClose() {

  	$('#shadow').customFadeOut(500);

  	$('#popup').fadeOut(500);

}



function popupSmall() {

  	$('#popup').css({width: '750px'/*, marginLeft: '0', top: '0'*/, padding: '5px'});

}



function popupBig() {

  	$('#popup').css({width: '800px'/*, marginLeft: '-450px'*/, top: '100px', padding: '50px'});

}



function show_addq() {

	showLoader();

	$('#popup').load('/faq/add_faq.html?template=82', function() {

  		//$('#shadow').customFadeIn(500);

  		popupSmall();

  		//$('#popup').fadeIn(500);

  		//$("#popup").slideto();

  		//$('body').slideto({highlight: false});

		$('#popup').center();

	});

	

}



function show_full(url){

	showLoader();

	$('#popup').load(url, function() {

  		//$('#shadow').customFadeIn(500);

  		popupSmall();

  		//$('#popup').fadeIn(500);

  		//$("#popup").slideto();

  		//$('body').slideto({highlight: false});

		$('#popup').center();

  		if( $('#popup').css('top') < 100 ) $('#popup').css('top', '100px');

	

		$('a.to_cart').click(function(){

			var form_name = 'to_cart_' + $(this).attr('rel');

			document.forms[form_name].submit();

			return false;

		});

	

	});

}



function cat_back() {

	if(history.length > 0)

		history.go(-1);

	else

		document.location.href = '/';

	return false;

}
