$(document).ready(function(){
	//remove borders from last elements in lists
	$('#news .articol:last,#cazare .articol:last, .comment:last, #footer div.left:last, #other-products li:last').css('border','none');
	
	//close top bar
	$('#top-bar a#close').click(function(){
		$('body').css('background-position',' 0 0');
		$('#top-bar').hide();
		
	});
	//item description same height
	sameHeights($('.item p'));
	
	//container same height
	var containerHeight = 0;
	$('.item').each(function(){
		var height = $(this).height();
		if(height>containerHeight){
			containerHeight = height;
		}
	});
	$('#container').height(containerHeight);
	
	//sidebar widgets border
	$('#sidebar > div').not('.clear,:last').css({'border-bottom':'1px solid #92cae8','padding-bottom':'10px','margin-bottom':'10px'});
	$('#sidebar .item').hover(function(){
		$('a.plus',this).show();
	},function(){
		$('a.plus',this).hide();
	});
	
	//remove left margin from zapari last item
	$('#zapari a:last').css('margin-right','0');
	
	//click on inputs
	$('#login input#username').focus(function(){
		var text  = $(this).val();
		
		if (text == 'username'){
			$(this).val('');
		} 
	}).blur(function(){
		var text  = $(this).val();
		if (text == ''){
			$(this).val('username');
		} 
		
	});
	$('#login input#parola').focus(function(){
		var text  = $(this).val();
		
		if (text == 'parola'){
			$(this).val('');
		} 
	}).blur(function(){
		var text  = $(this).val();
		if (text == ''){
			$(this).val('parola');
		} 
		
	});
	
	$('#login a.white').parent('form').width('458px');
	
	//next info  - strat zapada
	//init
	$('#info-zapada h4').not(':first').hide();
	$('#info-zapada .prev[rel="0"]').hide();
	var info_size = $('#info-zapada h4').length;
	
	//move to next
	$('#info-zapada .next').live('click',function(){
		var position = $(this).attr('rel');
		
		if(position<(info_size)){
			$(this).attr('rel',parseInt(position)+1);
			$('#info-zapada .prev').attr('rel',parseInt(position)-1);
		}else{
			$(this).hide();
			$('#info-zapada .prev').attr('rel',parseInt(position)-1);
		}
		$('#info-zapada .prev').show();
		$('#info-zapada h4').eq(position-2).hide();
		$('#info-zapada h4').eq(position-1).show();
	});
	
	//move to previous
	$('#info-zapada .prev').live('click',function(){
		var position = $(this).attr('rel');
		
		if(position==1){
			$(this).hide();
			$('#info-zapada .next').attr('rel',parseInt(position)+1);
			
		}else{
			$('#info-zapada .next').attr('rel',parseInt(position)+1);
			$(this).attr('rel',parseInt(position)-1);
		}
		$('#info-zapada .next').show();
		$('#info-zapada h4').eq(position).hide();
		$('#info-zapada h4').eq(position-1).show();
	});
	
	//remove padding for first cell
	$('table tr td:first-child').css('padding','10px 0');
	
	verticalCenter($('td .right_border'));
	
	
	
	
	
});
// imagini produs
$(window).load(function(){
	verticalCenter2($('.img_cont img'));
	verticalCenter2($('.img_cont2 img'));
	$('.img_cont3').click(function(){
		var href = $(this).attr('href');
		$('#img_produs .img_cont2 img').hide();
		$('#img_produs .img_cont2 img').attr('src',href);
		$('#img_produs .img_cont2 img').fadeIn();
		return false;
	});
});
//function for equal heights
function sameHeights($element){
	var elementHeight = 0;
	$element.each(function(){
		var height = $(this).height();
		if(height>elementHeight){
			elementHeight = height;
		}
	});
	$element.height(elementHeight);
}
function verticalCenter($element){
	var height = $element.height();
	var innerH = $element.children().innerHeight();
	var padd = Math.ceil((height-innerH)/2);
	$element.css('padding-top',padd+'px');
}
function verticalCenter2($element){
	var height = $element.height();
	var contH = $element.parent().height();
	if(contH > height){
		var margin = Math.ceil((contH - height)/2);
		$element.css('margin-top',margin+'px');
	}
	
}
//scrollable promo section
$(function(){
	//init
	var visibleItems = 2;
	var noitems = Math.ceil($('.item').length/visibleItems);
	var navi_width = 0;
	var margin =  parseInt($('#promo .item').css('marginRight'))
	var itemWidth = parseInt($('#promo .item').width())+margin;
	
	for(var i = 0;i<noitems;i++){
		$a = $('<a rel="'+i+'">'+i+'</a>');
		$('#promo .navi').append($a)
		navi_width = parseInt($a.width())+navi_width+5;
	}

	$('#promo .navi').width(navi_width);
	$('#promo .navi a:first').addClass('current');
	$('#promo .prev').css('visibility','hidden');
	
	$('#promo .navi a').live('click',function(){
		var number = parseInt($(this).attr('rel'));	
		showPage(number);	
	});
	
	$('#promo .next').live('click',function(){
		showNext();
	});
	$('#promo .prev').live('click',function(){
		showPrev();
	});
	
	function showPage(number){
		$('#items').stop(false,true);
		var newNumber = number*visibleItems; 
		var currentPage = $('#promo a.current').attr('rel');
		var currentDistance	= parseInt($('#items').css('left'));
		
		if((currentDistance/itemWidth)%visibleItems === 0){
			var distance = -itemWidth*visibleItems*(number-currentPage)+currentDistance;
		}else{
			var distance = -itemWidth*visibleItems*(number-currentPage)+currentDistance-itemWidth;
		}
		
		
		$('#items').animate({
			left:distance+'px'
		},600);
		
		showNavigation(newNumber);
	}
	function showNavigation(number){
		if (number == 0) {
			$('#promo .prev').css('visibility', 'hidden');
			$('#promo .next').css('visibility', 'visible');
		}
		else {
			if (number == noitems*visibleItems-visibleItems) {
				$('#promo .prev').css('visibility', 'visible');
				$('#promo .next').css('visibility', 'hidden');
			}
			else {
				$('#promo .prev').css('visibility', 'visible');
				$('#promo .next').css('visibility', 'visible');
			}
		}
		var pageNumber = Math.ceil(number/visibleItems);
		$('#promo .navi a').removeClass('current');
		$('#promo .navi a[rel="'+pageNumber+'"]').addClass('current');
	}
	function showNext(){
		$('#items').stop(false,true);
		var currentDistance	= parseInt($('#items').css('left'));
		var distance = -itemWidth+currentDistance;
		var number = -parseInt($('#items').css('left'))/itemWidth;
		showNavigation(number+1);
		$('#items').animate({
			left:distance+'px'
		},300);
				
	}
	
	function showPrev(){
		$('#items').stop(false,true);
		var currentDistance	= parseInt($('#items').css('left'));
		var distance = itemWidth+currentDistance;
		
		
		var number = -parseInt($('#items').css('left'))/itemWidth;
		
		showNavigation(number-1);
		if(number>0){
			$('#items').stop().animate({
				left:distance+'px'
			},300);	
		}	
		
	}
	
});
