var cartIsAnimating = false;
	function getSearch(prefix){
		$("#liveresults").empty().fadeIn().addClass("loading");
		$.post(prefix + "liveresults.php", { s: $("#s").val()}, function(data){
 
			$("#liveresults").html("" + data + "").fadeIn().removeClass("loading");

		});
	}
/*
	$(function(){

		$('#cart').hover(function(){
			if(!cartIsAnimating){
			cartIsAnimating = true;
				$('#cart_hover').fadeIn('fast');
			setTimeout(function(){
				cartIsAnimating = false;
			},200);

			}
		},
		function(){
				$('#cart_hover').fadeOut('fast');
		});
	});
	*/
	$(function(){
			$('.drop').click(function(){
				if( $(this).parent().parent().hasClass('selected')){
				 $(this).parent().siblings('ul').slideUp('fast', function(){
					$(this).parent().removeClass('selected');
				 });
				}else{
					$('.selected ul').slideUp('fast', function(){
						$(this).parent().removeClass('selected');
					})
					$(this).parent().siblings('ul').slideToggle('fast',function() {  
						$(this).parent().toggleClass('selected');  
					}).css({'display':'block'});
				}

			return false;
			});
	});

	$(function(){
			$('.drop1').click(function(){
				if( $(this).parent().parent().hasClass('selected1')){
				 $(this).parent().siblings('ul').slideUp('fast', function(){
					$(this).parent().removeClass('selected1');
				 });
				}else{
					$('.selected1 ul').slideUp('fast', function(){
						$(this).parent().removeClass('selected1');
					})
					$(this).parent().siblings('ul').slideToggle('fast',function() {  
						$(this).parent().toggleClass('selected1');  
					}).css({'display':'block'});
				}

			return false;
			});
					
	$("#propose-price").click(function(){
		$("#our-price").slideToggle('fast',function(){
			$("#propose").slideToggle('slow');
		});
		return false;
	});
	$("#close").click(function(){
		$("#propose").slideToggle('fast',function(){
			$("#our-price").slideToggle('slow');
		});
		return false;
	});
			
	});
	
	$(document).ready(function() {
	function getResults(){
		
		$.post("../funnel.php", {filter: "yes", manufecturer: $("#manufecturer").val(),display: $("#display").val(), cpu: $("#cpu").val(), hdd: $("#hdd").val(), ram: $("#ram").val(), prices: $("#prices").val(), promo: $("#promo").val()}, function(data){
			
			$("#numResults").stop().fadeOut('fast').html("<strong>" + data + " </strong>Продукта отговарят на вашият филтър").fadeIn();
			

		});
	}

	$("#s").blur(function(){
		$("#liveresults").fadeOut();
	});
	$("#manufecturer").change(function(){
	getResults();
	});
	$("#cpu").change(function(){
	getResults();
	});
	$("#hdd").change(function(){
	getResults();
	});
	$("#promo").change(function(){
	getResults();
	});
	$("#ram").change(function(){
	getResults();
	});
	$("#display").change(function(){
	getResults();
	});
	$("#prices").change(function(){
	getResults();
	});
	$("#promo").change(function(){
	if($(this).val()=="off"){
		$(this).val("on");
	}else{
		$(this).val("off");
	}
	getResults();
	});
	
});
function simple_tooltip(target_items, name){
 $(target_items).each(function(i){
		$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('alt')+"</p></div>");
		var my_tooltip = $("#"+name+i);

		$(this).removeAttr("title").mouseover(function(){
				my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(400);
		}).mousemove(function(kmouse){
				my_tooltip.css({left:kmouse.pageX+15, top:kmouse.pageY+15});
		}).mouseout(function(){
				my_tooltip.fadeOut(400);
		});
	});
}
$(document).ready(function(){
	 simple_tooltip(".badge","tooltip");
});

	$(function(){
		$('.badge').hover(function(){
			
			var tooltip = $(this).attr('alt');
			
			$("#tooltip").html(tooltip).stop().fadeIn();
			
		},function(){
			$("#tooltip").stop().fadeOut();
		});
	
	});
	
	$(function(){
	$('.firm').toggle();
		$("#optFirm").change(function(){
			$('.firm').toggle();
			$('.phys').toggle();
		});
	});
	
	$(function(){
		
	$('.required').change(function(){
	
		var curVal = $(this).attr('value');
		
			if(curVal != ""){
				if(curVal.indexOf("@") == -1 && $(this).hasClass("email") == true){
				
				}else{
					$(this).removeClass('required');
					$(this).addClass('completed');
				}
			}else{
				$(this).removeClass('completed')
				$(this).addClass('required');
			}
		
		}); 
	});
	
	$(function(){
	$("#errorMessage").hide();
		$('.checkoutfrm').submit(function(){
			$("#errorMessage").empty();
			var choice = $("#optFirm").attr('value');
			if(choice=="lice"){
			if($('#FirstName').attr('value')==""){
				$('#errorMessage').append("Моля въведете Име <br/>");
				var error = true;
			}
			if($('#LastName').attr('value')==""){
				$('#errorMessage').append("Моля въведете Фамилия <br/>");
				var error = true;
			}
			if($('#egn').attr('value')==""){
				$('#errorMessage').append("Моля въведете Egn <br/>");
				var error = true;
			}
			}else{
			if($('#FirmName').attr('value')==""){
				$('#errorMessage').append("Моля въведете Име на фирмата<br/>");
				var error = true;
			}
			if($('#FirmMol').attr('value')==""){
				$('#errorMessage').append("Моля въведете МОЛ на фирмата <br/>");
				var error = true;
			}
			if($('#FirmBulstat').attr('value')==""){
				$('#errorMessage').append("Моля въведете Булстат на фирмата <br/>");
				var error = true;
			}
			}
			if($('#Phone').attr('value')==""){
				$('#errorMessage').append("Моля въведете Телефон за връзка<br/>");
				var error = true;
			}
			if($('#Email').attr('value')==""){
				$('#errorMessage').append("Моля въведете E-mail <br/>");
				var error = true;
			}
			if($('#City').attr('value')==""){
				$('#errorMessage').append("Моля въведете Град<br/>");
				var error = true;
			}
			if($('#Address').attr('value')==""){
				$('#errorMessage').append("Моля въведете Адрес<br/>");
				var error = true;
			}
			if(error == true){
			$("#errorMessage").fadeIn();
			return false;
			}else{
			$("#errorMessage").fadeOut();
			}
		});
	
	});
		/*
	$(function(){
	
		$('.thumb_link').click(function(){
			$('.previewing').removeClass('previewing');
			$("#frame").addClass('loading');
			var newurl=$(this).attr("href");
			   var img = new Image();
				// load image
				$(img).load(function () {
					$(this).css('display','none'); // since .hide() failed in safari
					$("#frame").removeClass('loading').append(this);
					$("#main_photo").remove();
					$(this).fadeIn();
				}).error(function () {
					$(img).remove();
				}).attr('src', newurl).attr('id','main_photo');
				$(this).addClass('previewing');
		return false;
		});
	
	});
	*/
	$(function(){
	
		$('#grid_link').click(function(){
			$('.product_brief').animate({width:"303px"});
			$('.product_card').removeClass('align_left');
			return false;
		});
		$('#list_link').click(function(){
			$('.product_brief').animate({width:"643px",minHeight:"180px"});
			$('.product_card').addClass('align_left');
			return false;
		});
	
	$("#s").focus(function(){
		$(this).attr("value","");
	
		});
	});
	
$slideshow = {
    context: false,
    tabs: false,
    timeout: 5000,      // time before next slide appears (in ms)
    tabSpeed: 300,      // time it takes to slide in each slide (in ms) when clicking through tabs
    fx: 'fade',   // the slide effect to use
    
    init: function() {
        // set the context to help speed up selectors/improve performance
        this.context = $('#slideshow');
        
        // set tabs to current hard coded navigation items
        this.tabs = $('ul.slides-nav li', this.context);
        
        // remove hard coded navigation items from DOM 
        // because they aren't hooked up to jQuery cycle
        this.tabs.remove();
        
        // prepare slideshow and jQuery cycle tabs
        this.prepareSlideshow();
    },
    
    prepareSlideshow: function() {
        // initialise the jquery cycle plugin -
        // for information on the options set below go to: 
        // http://malsup.com/jquery/cycle/options.html
        $("div.slides > ul", $slideshow.context).cycle({
            fx: $slideshow.fx,
            timeout: $slideshow.timeout,
            speed: $slideshow.slideSpeed,
            fastOnEvent: $slideshow.tabSpeed,
            pager: $("ul.slides-nav", $slideshow.context),
            pagerAnchorBuilder: $slideshow.prepareTabs,
            before: $slideshow.activateTab,
            pauseOnPagerHover: true,
            pause: true
        });            
    },
    
    prepareTabs: function(i, slide) {
        // return markup from hardcoded tabs for use as jQuery cycle tabs
        // (attaches necessary jQuery cycle events to tabs)
        return $slideshow.tabs.eq(i);
    },

    activateTab: function(currentSlide, nextSlide) {
        // get the active tab
        var activeTab = $('a[href="#' + nextSlide.id + '"]', $slideshow.context);
        
        // if there is an active tab
        if(activeTab.length) {
            // remove active styling from all other tabs
            $slideshow.tabs.removeClass('on');
            
            // add active styling to active button
            activeTab.parent().addClass('on');
        }            
    }            
};

$(function() {
    // initialise the slideshow when the DOM is ready
     $slideshow.init();
});


$.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);
        
        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            
            return false;
        }
        
        $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
        
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};

$(document).ready(function () {
  $('.carousel').infiniteCarousel();
});
function storeAddress() {
	// Update user interface
	var email = $('#email').attr('value');
	$.get("library/store-address.php", {ajax: "true", email: $('#email').val()}, function(data){
	
			$("#response").stop().fadeOut('fast').html("<strong>" + data + " </strong>").fadeIn();
	
	});
}
$(document).ready(function () {
	$("#subscribe").click(function(){
		storeAddress();
		return false;
	})

});

