if ('undefined' === typeof window.console) {
	window.console = {
		log: function () {
			void(0);
		}
	};
}

jQuery.postJSON = function(url, data, callback) {
	jQuery.post(url, data, callback, 'json');
};

function onBefore() {
    var thisIndex = $(this).parent().children().index(this);
    $("#nav_sequence_1 div").removeClass("navButton1-selected");
    $("#nav_sequence_1 div").eq(thisIndex).addClass("navButton1-selected");
}

jQuery(function($) {
	var currContentHeight = 0;
	var maxContentHeight = 0;
	var currTitleHeight = 0;
	var maxTitleHeight = 0;
	var oneRowLevel = $(".oneRowLeveler");
	oneRowLevel.each(function() {
		var titleLeveler = $(this).find(".titleLeveler");
		//console.log(titleLeveler);
		titleLeveler.each(function() {
			currTitleHeight = $(this).height();
			if (currTitleHeight > maxTitleHeight) {
				maxTitleHeight = currTitleHeight;
			}
		});
		titleLeveler.height(maxTitleHeight);
		//console.log(maxTitleHeight);
		currTitleHeight = 0;
		maxTitleHeight = 0;

		var contentLeveler = $(this).find(".contentLeveler");
		//console.log(contentLeveler);
		contentLeveler.each(function() {
			currContentHeight = $(this).height();
			if (currContentHeight > maxContentHeight) {
				maxContentHeight = currContentHeight;
			}
		});
		contentLeveler.height(maxContentHeight);
		//console.log(maxContentHeight);
		currContentHeight = 0;
		maxContentHeight = 0;
	});
	
	$('.submitCheck')
		.attr('value', '1')
	;
	
	$('.noAction')
		.css('cursor', 'default')
		.click(function (e) {
			e.preventDefault();
		})
	;

	$('input:text').hint();
	
	$('#maincatCarousel, .accessoriesProducts')
		.css('display', 'block');

	$("#gallery").jcarousel({
        scroll: 5,
        animation: 800
    });
    $("#maincatCarousel").jcarousel({
        scroll: 6,
        animation: 800
    });
	
	if ($('#send_mail_ok').length > 0) {
		$.nyroModalManual({
			content: $('.nyroMessageContainer').html(),
			modal: true,
			closeSelector: '.button1',
			minWidth: 100,
 			minHeight: 100
		});
	}
	
	$('#sendWishlist')
		.click(function (e) {
			e.preventDefault();
			
			obj_this = $(this);
			
			if (!obj_this.attr('disabled')) {
				obj_wishlist = $('#wishlistEmail');
				
				obj_this
					.attr('disabled', 'disabled')
					.text('Изпращане');
				obj_wishlist
					.attr('disabled', 'disabled');

				$.postJSON(
					'/email-ajax-wishlist.php',
					{
						emailTo: $('#wishlistEmail').val()
					},
					function (receiveData) {
						obj_this
							.text('Изпрати')
							.removeAttr('disabled');
						obj_wishlist
							.removeAttr('disabled');
					
						if (1 === receiveData.status) {
							var smile_image = 'smiles_happy.jpg';
							var message_status = 'Списъкът е изпратен успешно!';
						}
						else {
							var smile_image = 'smiles_sad.jpg';
							var message_status = 'Грешка при изпращане на списъка!';
						}
	
						var obj_img = $('.nyroMessageContainer .nyroMessage img:eq(0)');
						var smile_path = obj_img
									.attr('src')
									.split('/');
						smile_path.pop();
						smile_path = smile_path.join('/');
						
						obj_img
							.attr('src', smile_path + '/' + smile_image)
							.siblings('p')
							.text(message_status);
	
					    $.nyroModalManual({
							content: $('.nyroMessageContainer').html(),
							modal: true,
							closeSelector: '.button1',
							minWidth: 100,
				 			minHeight: 100
						});
					}
				);
			}
		});
	
	/*
$('.basketOrder .deliveryPrices').click(function(e) {
	    $.nyroModalManual({
			content: $('.nyroMessageContainer1').html(),
			modal: true,
			closeSelector: '.button1',
			minWidth: 100,
 			minHeight: 100
		});
		return false;
	});
*/
	$('.basketOrder .deliveryZones').click(function(e) {
	    $.nyroModalManual({
			content: $('.nyroMessageContainer2').html(),
			modal: true,
			closeSelector: '.button1',
			minWidth: 100,
 			minHeight: 100
		});
		return false;
	});
	
    $('.inputContainer .inputText')
    	.focus(function() {
        	$(this)
        		.parents('.inputContainer')
        			.addClass('inputContainerFocused');
    });
    $('.inputContainer .inputText')
    	.blur(function() {
        	$(this)
        		.parents('.inputContainer')
        			.removeClass('inputContainerFocused');
    });
    
    $('.inputAreaContainer .inputArea')
    	.focus(function() {
        	$(this)
        		.parents('.inputAreaContainer')
        			.addClass('inputAreaContainerFocused');
    });
    
    $('.inputAreaContainer .inputArea')
    	.blur(function() {
        	$(this)
        		.parents('.inputAreaContainer')
        			.removeClass('inputAreaContainerFocused');
    });
    
    $('.buySellExch .typeSwitch')
    	.click(function(e) {
    		$(this)
    			.addClass('selected')
    			.siblings()
    				.removeClass('selected');
    		if ($(this).hasClass('sell')) {
    			$('#typeSwitchHidden').attr('value', '2');
    		}
    		else {
    			$('#typeSwitchHidden').attr('value', '1');
    		}
    		return false;
    					
    	})
    ;

	$("#discount .oneDiscount").hover(function() {
		$(this).css("background-position", "0 -92px");
	}, function() {
		$(this).css("background-position", "0 0");
	});

	var bounceActive = 0;
	$("#prodList .pic").hover(function() {
		if (bounceActive == 0){
			$(this).effect("bounce", { direction: "left", distance: 12, times: 3 }, 300);
			bounceActive = 1;
		}
	}, function() {
		bounceActive = 0;
	});
	
	var postDivs = $('.postDescription div');
	postDivs.hide();
	$('.postsTable tr').not('.postDescription')
		.click(function() {
			var targetDiv = $(this).next().find('div');
			if (targetDiv.is(':hidden')) {
				if ($('.postsTable .slidedDiv').length > 0) {
					$('.postsTable .slidedDiv')
						.removeClass('slidedDiv')
						.slideUp(500, function() {
							targetDiv
								.slideDown(500)
								.addClass('slidedDiv')
							;
						})
					;
				}
				else {
					targetDiv
						.slideDown(500)
						.addClass('slidedDiv')
					;
				}
			}
			else {
				targetDiv
					.slideUp(500)
					.removeClass('slidedDiv')
				;
			}
		})
	;
	
	var hintSection = $('.hint');
	var hintShower = $('.inputContainer input, .inputAreaContainer textarea');

	hintShower.focus(function() {
		var hint = $(this).next('.hint');
		var hintHeight = hint.height();
		var hintShowerXPos = $(this).position().left;
		var hintShowerYPos = $(this).position().top;
		hint.css("left", (hintShowerXPos + 335) + "px");
		hint.css("top", (hintShowerYPos - 12) + "px");
		hint.show();
	});
	hintShower.blur(function() {
		var hint = $(this).next('.hint');
		hint.hide();
	});
	
	/*
	$('#sequence_1').cycle({
	    fx:     'scrollUp',
	    timeout: 3000,
	    delay:  -2000
	});
	*/
	$('#sequence_1').cycle({
		fx:     'scrollUp',
		speed:  'normal',
	    timeout: 6000,
		pager:  '#nav',
		before:  onBefore, 
		pause: 1,
		pagerAnchorBuilder: function(idx, slide) {
			// return selector string for existing anchor
			//return '#nav_sequence_1 li:eq(' + idx + ') a';
			
			//var temp = '#nav_sequence_1 div:eq(' + idx + ')';
			//alert(temp);
			
			return '#nav_sequence_1 div:eq(' + idx + ')';
	    }
	});
	
	// Here an option - stop this cycle! after displaying selected slide
	$("#nav_sequence_1 div").click(function () {
		$("#nav_sequence_1 div").removeClass("navButton1-selected");
		$(this).addClass("navButton1-selected");
		$('#sequence_1').cycle('pause');
		
		// Resume sliding after NN miliseconds - timer
		var sequence_1_temp = function(){
			$("#sequence_1").cycle('resume');
		};
		setTimeout(sequence_1_temp, 10000);
    });
    
    // Here an another option - stop sliding on HOVER on the buttons!
	$("#nav_sequence_1 div").hover(
		function () {
			$("#sequence_1").cycle('pause');
		}, 
		function () {
			$("#sequence_1").cycle('resume');
		}
	);
	$('.navButton1').hover(function() {
		$(this).removeClass("navButton1").addClass("navButton1-over");
	}, function() {
		$(this).removeClass("navButton1-over").addClass("navButton1");
	});

	$("#cat_theme_1 .features .oneFeature img").each(function () {
		$(this).bind('mouseover', function() {
/* 				$(this).effect("pulsate", { times: -1 }, 500); */
			$(this)
				.animate({opacity: 0}, 250)
				.animate({opacity: 1}, 250);
		});
	});

	$("#cat_theme_1 .lessInfo").hide();
	$("#cat_theme_1 .moreInfo").click(function() {
		$("#prodList").slideUp(500, function() {
			$(".user_text_cat").slideDown(500, function() {
				$("#cat_theme_1 .moreInfo").fadeOut(function() {
					$("#cat_theme_1 .lessInfo").fadeIn();
				});
			});
		});
		return false;
	});
	$("#cat_theme_1 .lessInfo").click(function() {
		$(".user_text_cat").slideUp(500, function() {
			$("#prodList").slideDown(1000);
		});
		$("#cat_theme_1 .lessInfo").fadeOut(function() {
			$("#cat_theme_1 .moreInfo").fadeIn();
		});
		return false;
	});
	
	$('.noAction')
		.css('cursor', 'default')
		.click(function (e) {
			e.preventDefault();
		});
	$('.target_blank')
		.attr('target', '_blank');
		
	$('a.submitForm')
		.click(function (e) {
			e.preventDefault();
			$(this)
				.parents('form')
				.get(0)
				.submit();
		});
		
	$('#cart_sec .bind_cart .decreaseCount')
		.click(function (e) {
			e.preventDefault();
			
			cart_id = $(this)
				.parent()
				.parent()
				.attr('id');
			cart_id = cart_id
				.substring(1, cart_id.length);
			num_field = $(this).siblings('.number');
			prod_cnt = parseInt(num_field.text());
			if (prod_cnt > 1) {
				obj = $(this);
				$.getJSON('/cart/update/' + cart_id + '/-1', {}, function (data) {
						if (1 === data.status) {
							num_field
								.text(data.new_count);
							obj
								.parent()
								.siblings('.price_all')
								.text(data.new_price);
							$('#total_price')
								.text(data.total_price);
							if (data.new_count < 2) {
								obj
									.fadeTo(200, 0.1, function() {
										obj
											.css('visibility', 'hidden');
									});
							}
							
						}
				});
			}
		});
	$('#cart_sec .bind_cart .increaseCount')
		.click(function (e) {
			e.preventDefault();
			cart_id = $(this)
				.parent()
				.parent()
				.attr('id');
			cart_id = cart_id
				.substring(1, cart_id.length);
			num_field = $(this).siblings('.number');
			prod_cnt = parseInt(num_field.text());
			obj = $(this);
			$.getJSON('/cart/update/' + cart_id + '/1', {}, function (data) {
					if (1 === data.status) {
						num_field
							.text(data.new_count);
						obj
							.parent()
							.siblings('.price_all')
							.text(data.new_price);
						$('#total_price')
							.text(data.total_price);
						if (2 === data.new_count) {
							obj
								.siblings('.decreaseCount')
								.fadeTo(1, 0.1, function() {
									$(this)
										.css('visibility', 'visible')
										.fadeTo(200, 1);
								});
						}
					}
			});
		});
	$('#cart_sec .bind_cart .removeProduct')
		.click(function (e) {
			e.preventDefault();
			cart_id = $(this)
				.parent()
				.parent()
				.attr('id');
			cart_id = cart_id
				.substring(1, cart_id.length);
			$.getJSON('/cart/delete/' + cart_id, {}, function (data) {
					if (1 === data.status) {
						$('#total_price')
							.text(data.total_price);
						$('.r' + cart_id)
							.fadeOut(500, function () {
								if (data.empty) {
									$('#cart_sec .cart_list, #cart_sec .cart_buttons')
										.fadeOut(500, function () {
											$('#cart_empty')
												.slideDown(500);
										});
								}
							});
					}
			});
		});
		
	$('#cart_sec .cart_buttons .emptyCart')
		.click(function (e) {
			e.preventDefault();
			$.getJSON('/cart/empty', {}, function (data) {
					if (1 === data.status) {
						$('#cart_sec .cart_list, #cart_sec .cart_buttons')
							.fadeOut(500, function () {
								$('#cart_empty')
									.slideDown(500);
							});
					}
			});
		});
		
	$('#cart_sec .bind_wishlist .removeProduct')
		.click(function (e) {
			e.preventDefault();
			cart_id = $(this)
				.parent()
				.parent()
				.attr('id');
			cart_id = cart_id
				.substring(1, cart_id.length);
			$.getJSON('/my/favourites/delete/' + cart_id, {}, function (data) {
					if (1 === data.status) {
						$('#total_price')
							.text(data.total_price);
						$('.r' + cart_id)
							.fadeOut(500, function () {
								if (data.empty) {
									$('#cart_sec .cart_list, #cart_sec .cart_buttons, #cart_sec .favourite_send')
										.fadeOut(500, function () {
											$('#cart_empty')
												.slideDown(500);
										});
								}
							});
					}
			});
		});
		
		
	$('#cart_sec .bind_coming_soon_cart .decreaseCount')
		.click(function (e) {
			e.preventDefault();
			
			cart_id = $(this)
				.parent()
				.parent()
				.attr('id');
			cart_id = cart_id
				.substring(1, cart_id.length);
			num_field = $(this).siblings('.number');
			prod_cnt = parseInt(num_field.text());
			if (prod_cnt > 1) {
				obj = $(this);
				$.getJSON('/coming-soon-cart/update/' + cart_id + '/-1', {}, function (data) {
						if (1 === data.status) {
							num_field
								.text(data.new_count);
							obj
								.parent()
								.siblings('.price_all')
								.text(data.new_price);
							$('#total_price')
								.text(data.total_price);
							if (data.new_count < 2) {
								obj
									.fadeTo(200, 0.1, function() {
										obj
											.css('visibility', 'hidden');
									});
							}
							
						}
				});
			}
		});
		
	$('#cart_sec .bind_coming_soon_cart .increaseCount')
		.click(function (e) {
			e.preventDefault();
			cart_id = $(this)
				.parent()
				.parent()
				.attr('id');
			cart_id = cart_id
				.substring(1, cart_id.length);
			num_field = $(this).siblings('.number');
			prod_cnt = parseInt(num_field.text());
			obj = $(this);
			$.getJSON('/coming-soon-cart/update/' + cart_id + '/1', {}, function (data) {
					if (1 === data.status) {
						num_field
							.text(data.new_count);
						obj
							.parent()
							.siblings('.price_all')
							.text(data.new_price);
						$('#total_price')
							.text(data.total_price);
						if (2 === data.new_count) {
							obj
								.siblings('.decreaseCount')
								.fadeTo(1, 0.1, function() {
									$(this)
										.css('visibility', 'visible')
										.fadeTo(200, 1);
								});
						}
					}
			});
		});
	$('#cart_sec .bind_coming_soon_cart .removeProduct')
		.click(function (e) {
			e.preventDefault();
			cart_id = $(this)
				.parent()
				.parent()
				.attr('id');
			cart_id = cart_id
				.substring(1, cart_id.length);
			$.getJSON('/coming-soon-cart/delete/' + cart_id, {}, function (data) {
					if (1 === data.status) {
						$('#total_price')
							.text(data.total_price);
						$('.r' + cart_id)
							.fadeOut(500, function () {
								if (data.empty) {
									$('#cart_sec .cart_list, #cart_sec .cart_buttons')
										.fadeOut(500, function () {
											$('#cart_empty')
												.slideDown(500);
										});
								}
							});
					}
			});
		});
		
	$('#cart_sec .cart_buttons .emptyComingSoonCart')
		.click(function (e) {
			e.preventDefault();
			$.getJSON('/coming-soon-cart/empty', {}, function (data) {
					if (1 === data.status) {
						$('#cart_sec .cart_list, #cart_sec .cart_buttons')
							.fadeOut(500, function () {
								$('#cart_empty')
									.slideDown(500);
							});
					}
			});
		});	
		
		
		
	$('#cart_sec .bind_wishist .emptyCart')
		.click(function (e) {
			e.preventDefault();
			$.getJSON('/my/favourites/empty', {}, function (data) {
					if (1 === data.status) {
						$('#cart_sec .cart_list, #cart_sec .cart_buttons, #cart_sec .favourite_send')
							.fadeOut(500, function () {
								$('#cart_empty')
									.slideDown(500);
							});
					}
			});
		});
		
	nl_cnt = $('#newsletter_container');
	nl_addr = $('#newsletter_address');
	$('#newsletter_button')
		.click(function (e) {
			e.preventDefault();
			
			nl_cnt.block({
				message: 'Моля, изчакайте...',
				css: {
					width: '120px'
				}
			});
			
			$.postJSON(
				'/newsletter',
				{
					newsletter_email: nl_addr.val()
				},
				function (data) {
					setTimeout(
						function () {
							nl_cnt
								.unblock();
								
							nl_addr
								.val('')
								.focus();
						},
						5000
					);
					
					if (1 === data.status) {
						text_color = '#000';
						bg_color = '#0f0';
					}
					else if (0 === data.status) {
						text_color = '#fff';
						bg_color = '#f00';
					}
					else {
						return;
					}
					
					nl_cnt
						.block({
						message: data.message,
						css: {
							width: '200px',
							color: text_color,
							backgroundColor: bg_color
						}
					});
				}
			);
		});

	$(".myAccount .oneCategory").parent().css("background-color", "#fff").css("padding", "1px").corner("3px").children(".oneCategory").css("background-color", "#fff").corner("3px");
	$(".myAccount .oneCategory").hover(function() {
		$(this).parent().css("background-color", "#e0e0e0").children(".oneCategory").css("background-color", "#fff");
	}, function() {
		$(this).parent().css("background-color", "#fff").children(".oneCategory").css("background-color", "#fff");
	});
	
	var login = $('#loginSection');
	var loginMsg = $('#loginMsg').text();

	$('.loginBtn')
		.click(showLogin);
		
	$('.closeBtn, .loginLogo')
		.click(hideLogin);
	
	function showLogin (e) {
		if ('undefined' !== typeof e.preventDefault) {
			e.preventDefault();
		}

		login
			.animate({ top: '0px' }, 500);
	}
	
	function hideLogin (e) {
		if ('undefined' !== typeof e.preventDefault) {
			e.preventDefault();
		}

		login
			.animate({ top: '-105px' }, 500);
	}
	
	if (loginMsg.length > 0) {
		$.blockUI({
			message: loginMsg,
			css: {
				padding: '10px',
				fontSize: '14px',
				fontFamily: '"Trebuchet MS", Verdana, Arial',
				color: '#036a73',
				backgroundColor: '#4fd2e4'
			},
			timeout: 5000,
			onUnblock: showLogin
		});
	}
	
	/*
$(".cart_list tbody tr").hover(function() {
		console.log($(this).children());
		$(this).children().css("border", "1px solid #f00");
	}, function() {
		$(this).children().css("border", "1px solid #e2e2e2");
	});
*/

	$('#productAdd')
		.click(function (e) {
			e.preventDefault();
			$('#productForm')
				.submit();
		});

	$('.error')
		.each(function () {
			if (0 === $(this).html().length) {
				$(this).remove();
			}
		});
	$('.error')
		.siblings('.inputContainer, inputAreaContainer')
		.addClass('errorBorder')
	;
		
	$('#confirmCode')
		.click(function () {
			var src = $(this).attr('src');
			var pos = src.lastIndexOf('?');
			
			if (-1 === pos) {
			    pos = src.length;
			}
			
			src = src.substring(0, pos);
			
			$(this)
				.attr('src', src + '?' + Math.round(Math.random() * 9999999999));
		});
		
	$('#register_location')
		.change(function () {
			if ('-1' === $(this).val()) {
				$('#register_other_location:hidden')
					.slideDown(250, function () {
						$('#register_location')
							.removeAttr('name');
						$('#register_location_other')
							.attr('name', 'register_location')
							.focus()
							.select();
					});
			}
			else {
				$('#register_other_location:visible')
					.slideUp(250, function () {
						$('#register_location_other')
							.removeAttr('name');
						$('#register_location')
							.attr('name', 'register_location');
					});
			}
		});

	$('#addradd_location')
		.change(function () {
			if ('-1' === $(this).val()) {
				$('#addradd_other_location:hidden')
					.slideDown(250, function () {
						$('#addradd_location')
							.removeAttr('name');
						$('#addradd_location_other')
							.attr('name', 'addradd_location')
							.focus()
							.select();
					});
			}
			else {
				$('#addradd_other_location:visible')
					.slideUp(250, function () {
						$('#addradd_location_other')
							.removeAttr('name');
						$('#addradd_location')
							.attr('name', 'addradd_location');
					});
			}
		});

	$('#addredit_location')
		.change(function () {
			if ('-1' === $(this).val()) {
				$('#addredit_other_location:hidden')
					.slideDown(250, function () {
						$('#addredit_location')
							.removeAttr('name');
						$('#addredit_location_other')
							.attr('name', 'addredit_location')
							.focus()
							.select();
					});
			}
			else {
				$('#addredit_other_location:visible')
					.slideUp(250, function () {
						$('#addredit_location_other')
							.removeAttr('name');
						$('#addredit_location')
							.attr('name', 'addredit_location');
					});
			}
		});

	$('#invadd_type_company').click(function() {
		$('#name_field')
			.html('Име на фирмата: <span>*</span>');
		$('#in_field')
			.html('ИН: <span>*</span>');
		$('.error')
			.remove();
		$('.companyFields')
			.slideDown('fast');
	});
	
	$('#invadd_type_person').click(function() {
		$('#name_field')
			.html('Име: <span>*</span>');
		$('#in_field')
			.html('ЕГН: <span>*</span>');
		$('.error')
			.remove();
		$('.companyFields')
			.slideUp('fast');
	});

	$('#invedit_type_company').click(function() {
		$('#name_field')
			.html('Име на фирмата: <span>*</span>');
		$('#in_field')
			.html('ИН: <span>*</span>');
		$('.error')
			.remove();
		$('.companyFields')
			.slideDown('fast');
	});
	
	$('#invedit_type_person').click(function() {
		$('#name_field')
			.html('Име: <span>*</span>');
		$('#in_field')
			.html('ЕГН: <span>*</span>');
		$('.error')
			.remove();
		$('.companyFields')
			.slideUp('fast');
	});
	
	$('#order_location')
		.change(function () {
			if ('-1' === $(this).val()) {
				$('#order_other_location:hidden')
					.slideDown(250, function () {
						$('#order_location')
							.removeAttr('name');
						$('#order_location_other')
							.attr('name', 'order_location')
							.focus()
							.select();
					});
			}
			else {
				$('#order_other_location:visible')
					.slideUp(250, function () {
						$('#order_location_other')
							.removeAttr('name');
						$('#order_location')
							.attr('name', 'order_location');
					});
			}
		});

	$('#order_company').click(function() {
		$('#name_field')
			.html('Име на фирмата: <span>*</span>');
		$('#in_field')
			.html('ИН: <span>*</span>');
		$('.error')
			.remove();
		$('.companyFields')
			.slideDown('fast');
	});
	
	$('#order_person').click(function() {
		$('#name_field')
			.html('Име: <span>*</span>');
		$('#in_field')
			.html('ЕГН: <span>*</span>');
		$('.error')
			.remove();
		$('.companyFields')
			.slideUp('fast');
	});

    $('#lavaLampVariableImage').lavaLamp({
			fx: 'easeOutBack',
			speed: 800
			//startItem:1
		});
		
	$('#lavaLampVariableImageAuto').lavaLamp({
			fx: 'easeOutSine',
			speed: 800
		});
		
	$('#lavaLampVariableImageUnderCats').lavaLamp({
			fx: 'easeOutSine',
			speed: 800
		});
		
	$('#lavaLampVariableImageSoft').lavaLamp({
			fx: 'easeOutSine',
			speed: 800
		});
});

hs.registerOverlay(
	{
		thumbnailId: null,
		overlayId: 'controlbar',
		position: 'top right',
		hideOnMouseOut: true
	}
);
hs.graphicsDir = '/images/highslide/graphics/';
hs.outlineType = 'rounded-white';