
Cufon.replace('#menu li a', {hover: true});
Cufon.replace('#menu li a:hover', {color: '#fff'});
Cufon.replace('.section_title, #footer ul li h3, .aside h3');

window.onload = function(){
	$('.holder').jcarousel({
		wrap: 'both',
    	scroll: 1,
    	animation: 'slow',
    	auto: 30,
    	initCallback: mycarousel_initCallback,
		buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
	$('#partners').jcarousel({
		animation: 2500,
		wrap: 'last',
		auto: 8
	});
}

$(document).ready(function() {
	$('.submenu').each(function () {
		$(this).parent().hover(function() {
			$('.submenu:first', this).prev().addClass('on');
			$('.submenu:first', this).fadeIn(300);
		}, function() {
			$('.submenu:first', this).prev().removeClass('on');
			$('.submenu:first', this).fadeOut();
		});
	});
	
	$(".aside form input , #navigation form input").bind('focus blur',function(){
		show_hide_value(this);	
	});
	
	function show_hide_value(elem){
	   if ( $(elem).val() == '' ) { $(elem).val($(elem).attr('title')); }
	   else if ( $(elem).val() == $(elem).attr('title')) {$(elem).val(''); }
	}
	
//	$('#select').selectbox();
		
	$('#mycarousel').jcarousel({
    	wrap: 'circular',
    	scroll: 5,
    	animation: 'slow',
        initCallback: mycarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
	
	$('#side_menu ul.visible').parents("ul").addClass("visible").removeClass('hidden');
	$('#side_menu ul.visible').siblings().addClass("visible").removeClass('hidden');
	$('#side_menu ul.visible').parents("ul").siblings().addClass("visible").removeClass('hidden');
	$('span',$('.index').parents("ul").siblings('a')).html('-');
	$('.index span').html('-');

	/*
	 * Maхaмe бордерите според броя на продуктите 
	 * */
		
	$('#news #news_second li:nth-child(3n+3)').css({'background-image': 'none'});
	
	$('#flash_messages, .errors').each(function(){
		$(this).css('display', 'none');
		$(this).css('opacity', 0.65);
		$(this).fadeIn('slow');
		$('.close', $(this)).click(function(){ $($(this).parents('div')[0]).fadeOut(function(){	$(this).remove(); }); });
	});
	
	$('#animal, #breed, #submit select, #city, #neigborhood').selectmenu();
	$('#submit select').change(function() {
	    $('#submit').submit();
	});
	
	$('#filter_city select').change(function() {
	    $('#filter_city').submit();
	});
		
	
	$('#animals #animal').change(function() {
		if($(this).val()!="") {
			$('#animals').submit();
		}	
	});
	
	$('#breeds #breed').change(function() {
		if($(this).val()!="") {
			$('#breeds').submit();
		}	
	});
	
	$("#news_second li:nth-child(3n+3)").css("background", "none");
	
	var counter = $("#news_second li").length;
	if (counter > 0 && counter < 4) {
		$("#news_second li:nth-child(1) .text").css("border", "none");
		$("#news_second li:nth-child(2) .text").css("border", "none");
		$("#news_second li:nth-child(3) .text").css("border", "none");
	}
	
	if (counter > 3 && counter < 7) {
		$("#news_second li:nth-child(4) .text").css("border", "none");
		$("#news_second li:nth-child(5) .text").css("border", "none");
		$("#news_second li:nth-child(6) .text").css("border", "none");
	}
	
	if (counter > 6 && counter < 10) {
		$("#news_second li:nth-child(7) .text").css("border", "none");
		$("#news_second li:nth-child(8) .text").css("border", "none");
		$("#news_second li:nth-child(9) .text").css("border", "none");
	}
	
	$("#news_second li:last-child").css("background", "none");
	
	$(".product_list ul li:nth-child(1)").css("padding-top", "0");
	$(".product_list ul li:nth-child(2)").css("padding-top", "0");
	$(".product_list ul li:nth-child(3)").css("padding-top", "0");
	
/*	
	var prom_count = $('.prom_all ul li').length
	if(prom_count%2 == 0) {
		$('.prom_all ul li:last-child').css("border", "none");
		$(".prom_all ul li:nth-child("+(prom_count-1)+")").css("border", "none");
	} else {
		$('.prom_all ul li:last-child').css("border", "none");
	}
*/
	if ($.browser.msie) {
		$('#searchList li:last-child').css('border-bottom', 'none');
	}
	
	$('#reg_poll fieldset:first-child').removeClass('hidden');
	
	$('#reg_poll button').click(function(){
		ind = $('#reg_poll fieldset').index($('#reg_poll fieldset:visible'));
		$('#current_fieldset').html(ind+2);
		if($('#reg_poll fieldset:eq('+ind+') input:checked').length == 0) {
			alert('Моля изберете отговор.');
			return false;
		}
		
		if((ind+1) == $('#reg_poll fieldset').length) {
			add_mask();
			$('#reg_box').show();
			$('#reg_box').center(true);
		}
		
		$('#reg_poll fieldset:eq('+ind+')').addClass('hidden').next().removeClass('hidden');
		return false;
	});
	
	$('.close').click(function(){
		$('#reg_box').hide();
		$('#mask').remove();
	});
	
	$('#reg_box form ol li input[type="text"], #reg_box textarea').bind('focus blur',function(){
		show_hide_value(this);
	});
	
	$('#reg_box form').submit(function() {
		var params = $('#reg_poll').serialize();
		$("#reg_box form input").each(function(){
			if($(this).val() == $(this).attr('title')){
	       		$(this).val('');
	       	}
		});
		$.get(xhr_url+'?method=save_user',$('#reg_box form').serialize(),function(id){
			$.get(xhr_url+'?method=save_answer&id='+id, params, function(response){
				alert('Благодарим ви за отделеното време.');
				$('.poll').children().hide();
				$('.poll').append('<img src="'+_public+'images/layout/game-img.jpg" alt="Благодарим Ви за участието" id="poll_notice" />');
				$('.close').trigger('click');
			});
		});	
	//	location.reload();
		return false;
	});
	
	$('#reg_pop').click(function(){
		add_mask();
		$('#reg_box').show();
		$('#reg_box').center(true);
		return false;
	});
});

function mycarousel_initCallback(carousel) {	
	jQuery('.next').bind('click', function() {
        carousel.next();
        return false;
    });
	
	jQuery('.prev').bind('click', function() {
        carousel.prev();
        return false;
    });
	
	jQuery('#slide_fwd').bind('click', function() {
        carousel.next();
        return false;
    });
	
	jQuery('#slide_back').bind('click', function() {
        carousel.prev();
        return false;
    });
}

function add_mask()
{
	$('#mask').remove();
	$('body').append('<div id="mask"></div>');
	$('#mask').css({
		height : $('body').height()
	});
}

jQuery.fn.center = function (absolute) {
	return this.each(function () {
		var t = jQuery(this);

		t.css({
			position: absolute ? 'absolute' : 'fixed',
			left:  '50%',
			top:  '50%'
		}).css({
			marginLeft: '-' + (t.outerWidth() / 2) + 'px',
			marginTop: '-' + (t.outerHeight() / 2) + 'px'
		});

		if (absolute) {
			t.css({
				marginTop: parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(),
				marginLeft: parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
			});
		}
	});
};	

function show_hide_value(elem) {
	if ( $(elem).val() == '' ) { $(elem).val($(elem).attr('title')); }
	else if ( $(elem).val() == $(elem).attr('title')) {$(elem).val(''); }
}
