if(!console) {
    console = {};
}
if(!console.log) {
    console.log = function(){};
}

$(document).ready(function() {
	/* Search */
	$('.button-search').bind('click', function() {
		url = $('base').attr('href') + 'index.php?route=product/search';
				 
		var filter_name = $('input[name=\'filter_name\']').attr('value')
		
		if (filter_name) {
			url += '&filter_name=' + encodeURIComponent(filter_name);
		}
		
		location = url;
	});
	
	$('#header input[name=\'filter_name\']').keydown(function(e) {
		if (e.keyCode == 13) {
			url = $('base').attr('href') + 'index.php?route=product/search';
			 
			var filter_name = $('input[name=\'filter_name\']').attr('value')
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			
			location = url;
		}
	});
	
	/* Ajax Cart */
	$('#cart > .heading a').bind('click', function() {
		$('#cart').addClass('active');
		
		$.ajax({
			url: 'index.php?route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
				if (json['output']) {
					$('#cart .content').html(json['output']);
				}
			}
		});			
		
		$('#cart').bind('mouseleave', function() {
			$(this).removeClass('active');
		});
	});
	
	/* Mega Menu */
	$('#menu ul > li > a + div').each(function(index, element) {
		// IE6 & IE7 Fixes
		if ($.browser.msie && ($.browser.version == 7 || $.browser.version == 6)) {
			var category = $(element).find('a');
			var columns = $(element).find('ul').length;
			
			$(element).css('width', (columns * 143) + 'px');
			$(element).find('ul').css('float', 'left');
		}		
		
		var menu = $('#menu').offset();
		var dropdown = $(this).parent().offset();
		
		i = (dropdown.left + $(this).outerWidth()) - (menu.left + $('#menu').outerWidth());
		
		if (i > 0) {
			$(this).css('margin-left', '-' + (i + 5) + 'px');
		}
	});

	// IE6 & IE7 Fixes
	if ($.browser.msie) {
		if ($.browser.version <= 6) {
			$('#column-left + #column-right + #content, #column-left + #content').css('margin-left', '195px');
			
			$('#column-right + #content').css('margin-right', '195px');
		
			$('.box-category ul li a.active + ul').css('display', 'block');	
		}
		
		if ($.browser.version <= 7) {
			$('#menu > ul > li').bind('mouseover', function() {
				$(this).addClass('active');
			});
				
			$('#menu > ul > li').bind('mouseout', function() {
				$(this).removeClass('active');
			});	
		}
	}
	
	$('.success img, .warning img, .attention img, .information img').live('click', function() {
		$(this).parent().fadeOut('slow', function() {
			$(this).remove();
		});
	});	
	

	
});




function addToCart(product_id) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information, .error').remove();
			
			if (json['redirect']) {
				location = json['redirect'];
			}
			
			if (json['error']) {
				if (json['error']['warning']) {
					$('#notification').html('<div class="warning" style="display: none;">' + json['error']['warning'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
					
					$('.warning').fadeIn('slow');
					
					$('html, body').animate({ scrollTop: 0 }, 'slow');
				}
			}	 
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#cart_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

function removeCart(key) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'remove=' + key,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				$('#cart_total').html(json['total']);
				
				$('#cart .content').html(json['output']);
			}			
		}
	});
}

function removeVoucher(key) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'voucher=' + key,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				$('#cart_total').html(json['total']);
				
				$('#cart .content').html(json['output']);
			}			
		}
	});
}

function addToWishList(product_id) {
	$.ajax({
		url: 'index.php?route=account/wishlist/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#wishlist_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 				
			}	
		}
	});
}

function addToCompare(product_id) { 
	$.ajax({
		url: 'index.php?route=product/compare/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$('#notification').html('<div class="success" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				
				$('.success').fadeIn('slow');
				
				$('#compare_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}





/* by joro for read more and add class last on offer box */

$(document).ready(function(){
	$('.content .description *:pageBreak').each(function(i) {
		$(this).addClass('page-break');
		var $parent = $(this).parents('.description').attr('id', 'read-more-auto-generated-'+i);
		var $this = $(this);
		var $eee = $(this);
		var $height = 0;
		while($eee.size()) {
			$height = $height + parseInt($eee.outerHeight(true));
			$eee = $eee.prev();
		}
		//var $height = ($this.offset().top - $parent.offset().top) + $this.height();
		var $original_height = $parent.height();
		var $href = $('<a class="read-more-auto-generated">Прочети още</a>').css({
			'float': 'right',
			'cursor': 'pointer'
		}).click(function(){
			var el = $('#read-more-auto-generated-'+i);
			if(el.height() == $height) {
				el.animate({height: $original_height}, 2000);
				$href.find('.read-more-auto-generated').html('Скрии');
			} else {
				el.animate({height: $height}, 2000);
				$href.find('.read-more-auto-generated').html('Прочети още');
			}
			return false;
		});
		$parent.css({
			'overflow': 'hidden',
			'height': $height
		}).after($href);
	});
	
	///////////////////////////
	$('.content').each(function(){
		var $content = $(this);
		var num = $content.is(':hasSidebar');
		$('.box-landing-holder, .content > .left', $content).each(function(){
			$('div.offer', this).each(function(i){
				if( (num && i%4==3) || (!num && i%5==4) ) {
					$(this).addClass('last');
				}
			});
			$('div.dest', this).each(function(i){
				if( (num && i%4==3) || (!num && i%5==4) ) {
					$(this).addClass('last');
				}
			});
			$('div.offer-big', this).each(function(i){
				if( (num && i%2==1) || (!num && i%2==1) ) {
					$(this).addClass('last');
				}
			});
		});
		
	});
	
	///////////////////////////
	$('.box-inner div:moreResults').each(function(i){
		var $parent = $(this).attr('id', 'acordeon-auto-generated-'+i);
		var $eee = $('.limit-row-box', $parent);
		
		if($eee.size()) {
			var $height = 0;
			while($eee.size()) {
				$height = $height + parseInt($eee.outerHeight(true));
				$eee = $eee.prev();
			}
			var $original_height = $parent.height();
			
			var $href = $('<div class="sep"></div><div style="text-align:center;"><a class="acordeon-auto-generated">Виж още</a></div>').css({
				//'float': 'right',
				'cursor': 'pointer'
			}).click(function(){
				var el = $('#acordeon-auto-generated-'+i);
				if(el.height() == $height) {
					el.animate({height: $original_height}, 2000);
					$href.find('.acordeon-auto-generated').html('Скрии');
				} else {
					el.animate({height: $height}, 2000);
					$href.find('.acordeon-auto-generated').html('Виж още');
				}
				return false;
			});
			$parent.css({
				'overflow': 'hidden',
				'height': $height
			}).after($href);
		}
		
	});
	
});

// custom selectors
$.expr[':'].pageBreak = function(obj) {
	return ($(obj).css('page-break-after') == 'always');
};

$.expr[':'].moreResults = function(obj) {
	return $(obj).hasClass('more-result-show');
};

$.expr[':'].hasSidebar = function(obj) {
	return $(obj).find('.sidebar').size();
};

//plug
(function($){
	$.fn.scrollTo = function(callback) {
		var el = this;
		if($(el).size() > 0) {
			$('html, body').animate({scrollTop:$(el).offset().top}, 'slow', (jQuery.isFunction(callback) ? callback(el) : function() { }));
		}
		return false;
	};
	
	$.fn.scrollToCenter = function(callback) {
		var el = this;
		if($(el).size() > 0) {
			$('html, body').animate({scrollTop: ($(el).offset().top - ($(window).height()/2))}, 'slow', (jQuery.isFunction(callback) ? callback(el) : function() { }));
		}
		return false;
	};
})(jQuery);




//////////////////////

$.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

function getIEVersionNumber()
{
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");

    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}

function loaderShow()
{

	var browser=navigator.appName;
	if (browser != "Microsoft Internet Explorer")
	{
		$.blockUI({message: '<div id="site-loader"></div>', css: {top:'40%', left:'45%'}});
		$('.blockUI.blockMsg').center();
	}
	else
	{
		$(document).ready(function() {
			var IEVersion = getIEVersionNumber();

			if(IEVersion > 7)
			{
				
				$.blockUI({message: '<div id="site-loader"></div>',  centerY: true, centerX: true, css: {top:'40%', left:'45%'}});
				$('.blockUI.blockMsg').center();
				
			}
		});

	}

    // setTimeout('test()', '7000');
}

function unblockUI()
{
	$.unblockUI();
}

$(document).ready(function(){
	var accordion = $( ".accordion-holder" );
	var hash = window.location.hash;
	var options = {};
	if(hash) {
		active = 0;
		accordion.find('a.cg1').each(function(i){
			if(this.id == 'faq_id_' + hash.replace('#','')) {
				active = i;
			}
		});
		
		if(active > -1) {
			options.active = active;
		}
	}
	accordion.accordion(options);
});
