$(document).ready(function(){
	/*
	** Teksti ne placehoder te inputeve
	*/
	$('body').find('input').each(function(){
		if ($(this).prop('placeholder') != '') {
			$(this).val($(this).attr('placeholder'));
			$(this).addClass('inactive');
		}
	});
	
	$('body').find('input').click(function(){
		if ($(this).prop('placeholder') != '') {
			$(this).removeClass('inactive');
			
			placeholder = $(this).prop('placeholder');
			if ($(this).val() == placeholder) {
				$(this).val('');
			}
		}
	});
	
	$('body').find('input').blur(function(){
		if ($(this).prop('placeholder') != '') {
			placeholder = $(this).prop('placeholder');
			if ($(this).val() == '') {
				$(this).val(placeholder);
				$(this).addClass('inactive');
			}
		}
	});
	
	/*
	** Fshehja e checkbox-eve te filtrimit
	*/
	$('#filter > div label').find('a').each(function(){
		if ($(this).parent().children('input[type="checkbox"]').prop('checked')) {
			$(this).addClass('active');
		}
		
		$(this).parent().children('input[type="checkbox"]').hide();
	});
	
	/*
	** Aktivizimi i checkbox-eve te filtrimit kur klikohen linket
	*/
	$('#filter > div label').find('a').click(function(){
		if ($(this).hasClass('active')) {
			$(this).removeClass('active');
			$(this).parent().children('input').prop('checked', false);
		} else {
			$(this).addClass('active');
			$(this).parent().children('input').prop('checked', true);
		}
		return false;
	});
	
	/*
	** Hapja dhe mbyllja e panelit te filtrimit kur klikohet butoni
	*/
	var down_arrow = $('#filter > a').find('img').attr('src');
	
	$('#filter > a').click(function(){
		var link = $(this);
		$('#filter > div').slideToggle(function(){
			if ($(this).is(':visible')) {
				link.find('img').prop('src', link.find('img').attr('alt'));
				link.animate({'background-color': '#f0a15a'}, 200);
			} else {
				link.find('img').prop('src', down_arrow);
				link.animate({'background-color': '#fbb426'}, 200);
			}
		});
		return false;
	});
	
	/*
	** Hapja e etiketave kur klikohet kategoria ne filtrim
	*/
	$('#filter li > a').click(function(){
		$('#filter li > a').hide();
		
		cat_id = $(this).parent().attr('cat');
		$('div#' + cat_id).show();
		
		var price_range = $('div#' + cat_id).attr('price-range');
		var price_range = price_range.split(':');
		
		var min_price = parseInt(price_range[0]);
		var max_price = parseInt(price_range[1]);
		
		$('#slider-range').slider({
			min: min_price,
			max: max_price
		});
		
		$('#slider-range').slider('values', 0, min_price);
		$('#slider-range').slider('values', 1, max_price);
		
		$('#amount').val(min_price + ' - ' + max_price + ' Leke');
		
		$('#filter-cat-title').html('&bull; Etiketat &bull;');
		
		return false;
	});
	
	$('a.back-to-cat').click(function(){
		$('div.filter-tags').hide();
		$('#filter li > a').show();
		
		$('#filter-cat-title').html('&bull; Kategorite &bull;');
		
		return false;
	});
	
	/*
	** Range Slider (jQueryUI) per cmimin ne filtrim
	*/
	$('#slider-range').slider({
		range: true,
		min: 0,
		max: 50000,
		step: 100,
		values: [0, 30000],
		slide: function (event, ui) {
			$('#amount').val(ui.values[0] + " - " + ui.values[1] + ' Leke');
		}
	});
	
	/*
	** Shtimi i dyqanit si i preferuar
	*/
	$('a.star').click(function(){
		var star = $(this);
		var shop_id = $(this).attr('rel');
		
		$.get(PATH + 'ajax.php?action=favoriteShop&id=' + shop_id, function(data){
			star.tipsy('hide');
			star.fadeOut().delay(500).remove();
		});
		
		return false;
	});
	
	/*
	** Heqja e dyqanit nga te preferuar
	*/
	$('a.remove').click(function(){
		var remove = $(this);
		var shop_id = $(this).attr('rel');
		
		$.get(PATH + 'ajax.php?action=removeFavorite&id=' + shop_id, function(data){
			if (remove.parent().hasClass('favorite')) {
				remove.tipsy('hide');
				remove.parent().fadeOut().delay(500).remove();
			} else {
				remove.tipsy('hide');
				remove.fadeOut().delay(500).remove();
			}
			
			if ($('div.home-favorites').find('.shop').length == 0) {
				$('.home-favorites').hide();
				if (remove.parent().hasClass('favorite')) {
					$('ul.tab-buttons').find('li:first-child').addClass('active');
					$('div.tabs > div:eq(0)').slideDown('slow');
				}
			}
		});
		
		return false;
	});
	
	/*
	** Butonat e preferimit ne faqen e dyqaneve
	*/
	$('#shop-social-favorite a.star, #shop-social a.remove').click(function(){
		$('#shop-social-favorite').remove();
	});
	
	/*
	** Vendosja e ndaresit vertikal midis kolonave
	*/
	if ($('#sep').length !== 0) {
		$('#sep').css({
			left: $('aside').position().left,
			height: $('#content').outerHeight() + 'px'
		});
	}
	
	$(window).resize(function(){
		$('#sep').css({
			left: $('aside').position().left,
			height: $('#content').outerHeight() + 'px'
		});
	});
	
	/*
	** Nderrimi i gjendjes se listimit kur vendoset mouse siper
	*/
	$('div.shop').hover(
		function () {
			$(this).stop(true, true).animate({backgroundColor : '#eff8fd'}, 500);
			$(this).find('p.mini-details').find('span').stop(true, true).animate({
				'backgroundColor': '#72bde7',
				'color': '#fff'
			}, 500);
			$(this).children('a.star, a.remove').stop(true, true).fadeIn(500);
			$(this).children('a.more').show();
			$(this).children('a.more').stop(true, true).animate({
				'right': '0'
			}, 300);
		},
		function () {
			$(this).stop(true, true).animate({backgroundColor : '#fff'}, 200);
			$(this).find('p.mini-details').find('span').stop(true, true).animate({
				'backgroundColor': '#eee',
				'color': '#999'
			}, 200);
			$(this).children('a.star, a.remove').stop(true, true).fadeOut(200);
			$(this).children('a.more').stop(true, true).animate({
				'right': '-200px'
			}, 200, function(){
				$(this).hide();
			});
		}
	);
	
	/*
	** Slideri
	*/
	$('#slider-images div:first-child').addClass('slider-active');
	
	var image_width = $('#canvas').width();
	var total_images = $('#slider-images div').size();
	var canvas_width = image_width * total_images;
	
	$('#slider-images').css({'width' : canvas_width});
	
	slide = function(){
		var current_id = $active.attr('rel') - 1;
		var canvas_position = current_id * image_width;
		
		$('#slider-images div').removeClass('slider-active');
		$active.addClass('slider-active');
	
		$('#slider-images').animate({
			left: -canvas_position
		}, 700);
	
	}; 

	slideSwitch = function(){
		play = setInterval(function(){
			$active = $('#slider-images div.slider-active').next();
			if ($active.length === 0) {
				$active = $('#slider-images div:first-child');
			}
			slide();
		}, 5000);
	};
	
	slideSwitch();
	
	$('#slider-images div').hover(function() {
		clearInterval(play);
	}, function() {
		slideSwitch();
	});
	
	$('#slider-next').click(function() {
		$active = $('#slider-images div.slider-active').next();
		if ($active.length === 0) {
			$active = $('#slider-images > div:first-child');
		}
		
		clearInterval(play);
		slide();
		slideSwitch();
		return false;
	});
	
	$('#slider-prev').click(function() {
		$active = $('#slider-images div.slider-active').prev();
		if ($active.length === 0) {
			$active = $('#slider-images > div:last-child');
		}
		
		clearInterval(play);
		slide();
		slideSwitch();
		return false;
	});
	
	/*
	** Tabet
	*/
	$('ul.tab-buttons a').click(function() {
		var pos = $(this).parent().index();
		
		if (!$('div.tabs > div:eq(' + pos + ')').is(':visible')) {
			$('ul.tab-buttons li').removeClass('active');
			$(this).parent().addClass('active');
			
			$('div.tabs > div').slideUp('slow');
			$('div.tabs > div:eq(' + pos + ')').slideDown('slow');
		}
		return false;
	});
	
	/*
	** Tipsy
	*/
	$('a.tipsy').tipsy({
		fade: true,
		gravity: 's',
		opacity: 1
	});
	
	/*
	** Lightbox
	*/
	$('a.lightbox').lightBox({
		imageLoading: PATH + 'assets/img/lightbox-loading.gif',
		imageBtnClose: PATH + 'assets/img/lightbox-close.gif',
		imageBtnPrev: PATH + 'assets/img/lightbox-prev.gif',
		imageBtnNext: PATH + 'assets/img/lightbox-next.gif',
		txtImage: 'Foto',
		txtOf: 'nga'
	});
	
	/*
	** Heqja e ndaresit te fundit te tabeles se kategorive
	*/
	$('#shop-refs').find('tr.divider:last-child').remove();
	
});
