jQuery(document).ready(function($){
	
	//Nubmers Only
	$("#q-box").keydown(function(e){

        var key = e.charCode || e.keyCode || 0;
        // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
        return (
            key == 8 || 
            key == 9 ||
            key == 46 ||
            (key >= 37 && key <= 40) ||
            (key >= 48 && key <= 57) ||
            (key >= 96 && key <= 105));
    });

	buttonOverState('.contactForm .divInputSubmit', 'inputSubmitOver');
	buttonOverState('.div-input-submit', 'input-submit-hovered');
	
	$('.home-carousel-holder span').each(function(){
		var mrgleft = ($(this).parent().width() - $(this).width())/2;
		//$(this).css('margin-left',mrgleft);
	})
	
	$(".teaser").cycle({
	    fx: 'fade',
	    speed: 800,
	    timeout: 6000, // > 0(miliseconds) to scroll automaticly, 0 for manual scroll
	    pause: false,
	    pager: '#teaser-nav'
	});
	
	var maxh = 0;
	$('.home-carousel-holder span').each(function(){
		if($(this).height()>maxh)maxh = $(this).height()
	})
	$('.home-carousel-holder span').height(maxh)
	
	/*
	$('.search_quick .inputText').each(function(){
		$(this).watermark( $(this).val(), {'useNative': false} );
	});
	*/
	
	$("#tabs > ul").tabs({selected:0});

	var biggest_li_element = 0;
	$(".ui-tabs-nav li").each(function(){
		if($(this).outerHeight() > biggest_li_element)
			biggest_li_element = $(this).outerHeight();
	});
	
	if(biggest_li_element > 0){
		
		$(".ui-tabs-nav li").css('height', biggest_li_element);
	}
	
	jQuery('.carousel').jcarousel({
        // Configuration goes here
    });
    
   $(".prod-img").attr("href", $(".carousel-small a").eq(0).attr('href'))
    
	$(".carousel-small a").hover(function() {
		var thisHREF = $(this).attr("href");
		//var carouselItemID = $(this).attr("id");
		$(".prod-img").attr("href", thisHREF); // change Href of the big pic with the href of the biggest pic

		var MidHref = $(this).attr("rel"); // copy the href of a in the small carousel
		//alert(MidHref);
		
		// Fade only if src is different
		if ($(".prod-img img").attr('src') != MidHref) {		
			$(".prod-img img").fadeTo(200, 0.1, function() {
				$(this).attr("src", MidHref).fadeTo(200, 1);
			});	
		}
	});
	
	/* Clickink on Medium pic activates click on the same Small pic (and shows Big pic :) ) */
	$(".product-details .item-image").click(function (event) {
		event.preventDefault(); // If not set on click you'll go to default href
		var i = 0;
		$(".carousel-small a").each(function () {
			if ($(this).attr('href') == $('.prod-img').attr('href')) {
				
				if(i==0){
				$(this).trigger('click');
				i++;
				}
			}
			else {
			}
		});
	});
	
	$('#payment-method,.towns-select select').styledSelect();
	
	//color select
	$('#of').html($('.option').eq(0).html());

	$('#select-val').val($('.color-text',$('.option').eq(0)).text())
	$('#select-val-code').val($('#color-code',$('.option').eq(0)).val())

	$('.option').eq(0).addClass('selected')
	$('.color-select').click(function(){
		if($(this).attr('id')=='open'){
			$(this).removeAttr('id');
			$('#of').html($('.color-select .selected').html());
		}else{
			$(this).attr('id','open');
			$('#of').html('');
		}
	})
	
	$('.color-select li').click(function(){

			$('#of').html($(this).html());
			$('.color-select li').removeClass('selected')

/*
			$('#select-val').val($('.color-text',$(this)).text())
			$('#select-val-code').val($('#color-code',$(this)).val())
*/

			$('#select-val').val($('.color-text',$(this)).text())
			$('#select-val-code').val($('#color-code',$(this)).val())

			
			$(this).addClass('selected')
	})
	
	//float box
	
	if($('.left-menu').length != 0 && $('#basket_counter_box').html() != 0){
	
		$("#floatbox").css('left',$('.left-menu').offset().left)
		$("#floatbox").css('display','block');
		$("#floatbox").animate({top:$(window).height()-150+$(window).scrollTop()},1000);
		$(window).scroll(function(){
			var tp = $(window).scrollTop() + $(window).height()-150;
			$("#floatbox").stop().animate({top:tp},100);
		}).resize(function(){
			var tp = $(window).scrollTop() + $(window).height()-150;
			$("#floatbox").stop().animate({top:tp},100);
			$("#floatbox").css('left',$('.left-menu').offset().left)
		})
	}	


	$(window).bind('hashchange', function(){

        newHash = window.location.hash.substring(1);

        if (newHash) {

/* 			$('.jqSelectByClass').hide('fast'); */
			$('.jqSelectByClass').css('display', 'none');
						
				var city = newHash.split("-");
	
				$('#dis-'+city[1]).show('fast');
	
	
				if($('#c'+city[1])){
	
					//Get coords from hidden by id
					var coords = $('#c'+city[1]).val().split(',');
					
					$('#map_pin').animate({
									top: coords[0].replace("px", ""),
									left: coords[1].replace("px", "")
								}, 'fast');
				
				}
	
				if($("#SS1").length != 0){
				
					//update select
					$("#SS1").remove();
					$("#city_selector").val(urldecode(newHash)).attr('selected', 'selected');
					$("#city_selector").styledSelect();
				}			
			
        };
        
    });
    

	//Bind on city selector
    $("#city_selector").change(function () {    	
		window.location.hash = '#'+$(this).attr('value');
	});
	

	//On page refresh
    $(window).trigger('hashchange');
    
    
	$("#request-form").submit(function() {

		$.ajax({  
			type: "GET",  
			url: "/ajax_basket.php",  
			data: $(this).serialize(),  
			success: function(res) {  

				$('#basket_counter_box').html(res);
				Boxy.alert("<table><tr><td><img src='/images/icon_ok.gif'></td><td>&nbsp;&nbsp;Продукта е добавен успешно!</td></tr></table>", null, null);
				float_box();
			}
		});  
		return false;  	

	});


	$('#boxy').boxy();
	

	//Bind left menu effects
	
	$('.left-menu .hidden').mouseenter(function(){ //Enter
		$(this).addClass('opened').children('ul').slideDown('fast');
	})

	//Close menu element on menu.mouseleave
	$('.left-menu').mouseleave(function(){
		$(this).find('.hidden').removeClass('opened').children('ul').slideUp();
	});


}); //END jQ ready

function float_box(){

	$("#floatbox").css('left',$('.left-menu').offset().left)
	$("#floatbox").css('display','block');
	$("#floatbox").animate({top:$(window).height()-150+$(window).scrollTop()},1000);
	$(window).scroll(function(){
		var tp = $(window).scrollTop() + $(window).height()-150;
		$("#floatbox").stop().animate({top:tp},100);
	}).resize(function(){
		var tp = $(window).scrollTop() + $(window).height()-150;
		$("#floatbox").stop().animate({top:tp},100);
		$("#floatbox").css('left',$('.left-menu').offset().left)
	})
}

/* OnOver - add class for IE - Should be used on elements different from Links */
function buttonOverState(selector, className)
{
	/* Hover over the login button */
	$(selector).hover(function(){
		$(this).addClass(className);
	},
	function(){
		$(this).removeClass(className);
	});
}

function urldecode (str) {

    return decodeURIComponent((str + '').replace(/\+/g, '%20'));
}
