
function removeFromFavorite(params){
	
	url = remove_from_favouvorites_url + params;
	$.ajax({
		type: "GET",
		url: url,
	 	success: function(html){
			var items_ocunt = $('h5.wishlist-list-title span.wish-list-title-items').get(0);
			var sub_title_old_val =  parseInt($(items_ocunt).html());
			$('h5.wishlist-list-title span.wish-list-title-items').html(sub_title_old_val-1);
		
			alert('Вие премахнахте този продукт от Предпочитани!');
	  	}
	});
};

var dateObject = new Date();

function addToFavorite(params,many){
	
	url = add_to_favouvorites_url + params + '?time='+dateObject.getTime() ;
	
	$.ajax({
		type: "GET",
		url: url,
	 	success: function(html){

	 		if(!html) return;

			var items_ocunt = $('h5.wishlist-list-title span.wish-list-title-items').get(0);
			var sub_title_old_val =  parseInt($(items_ocunt).html());
			$('h5.wishlist-list-title span.wish-list-title-items').html(sub_title_old_val+1);

			$('.wish-wed-list .wlist-content .wlist-content-holder').prepend(html);

			$('div.wish-wed-list div.item-teaser a.rem-from-fav:first').click(function(){
				removeFromFavorite($(this).attr('href'));
				$(this).parent().parent().remove();
				//alert('Removed item form favourites');
				return false;
			});
			
			if(!many){
				alert('Вие добавихте този продукт в Предпочитани!');
			}
	  	}
	});
};

function quickViewWindow(elem){
	var pop_up_window = $('div#pop-up-window');
	$(pop_up_window).html($('div#pop-up-shel').html());
	//$('div#loader').show();
	$('a.close',pop_up_window).click(function(){
		$(pop_up_window).dialog('close');
		return false;
	});
	$('div#loader',pop_up_window).hide();
	$('div#pop-up-window-conten',pop_up_window).html($(elem).next().html());
	
	$('#product-quick-view a.img-thumb',pop_up_window).click(function(){
		var new_src = $(this).attr('href');
		var big_img = $('#product-quick-view .big-gallery-img img',pop_up_window);
		$(big_img).attr('src',new_src);
		$(big_img).show();
		return false;
	});
	//$('div#pop-up-window').append($(elem).next().html());

	$('a.add-to-fav',pop_up_window).click(function(){
		addToFavorite($(this).attr('href'),false);
		//alert('Uspeshno dobawihte produkt w predpo4itani!');
		return false;
	});

	$(pop_up_window).dialog('option', 'height', 555);
	$(pop_up_window).dialog('option', 'width', 440);
	$(pop_up_window).dialog('open');
}

/* wedding_list_url 
 * the function is coled form customize-check-box script 
 * */
cfeOnChecked = function(elm, checked){
	
	url = wedding_list_url;
	url += (checked)?'addItem/':'delItem/';
	url += $(elm).attr('name');
	$.ajax({
		type: "GET",
		url: url,
	 	success: function(html){

			var title_old_val =  parseInt($('#wedding-list span.wedding-list-title-checked-items').html());
	 		tLI = $(elm).parent().get(0);
	 		tUL = $(tLI).parent().get(0);
	 		tH5 = $(tUL).prev();

			var sub_title =$('a span.wedding-list-sub-title-checked-items',tH5);
			var sub_title_old_val =  parseInt($(sub_title).html());

			if(checked){
				title_new_val =title_old_val+1;
				sub_title_new_val = sub_title_old_val+1;	
			}else{
				title_new_val = title_old_val-1;
				sub_title_new_val = sub_title_old_val-1;					
			}

			$('span.wedding-list-title-checked-items').html(title_new_val);
			$(sub_title).html(sub_title_new_val);
	  	}
	});
};

$(document).ready(function(){

	/* social networks */
	$('#share-in-social-networcks-open,#share-in-social-networcks-close').click(function(){
		/*var top = $(this).attr('top');
		var left = $(this).attr('left');
		$('#social-networks').attr('top',top);
		$('#social-networks').attr('left',left);*/
		$('#social-networks').toggle();
		return false;
	});
	$('#social-networks').hover(null,function(){
		$(this).toggle();
	});
	
	/* weding list */
	$('#wedding-list h5.wedding-list-title').click(function(){
		if ($('#wedding-list div.wish-wed-list').is(":hidden")) {
			if ($('#wishlist div.wish-wed-list').is(":show")) {
				$('#wishlist div.wish-wed-list').slideUp("slow");
			}
			$('#wedding-list div.wish-wed-list').slideDown("slow");
		}else{
			$('#wedding-list div.wish-wed-list').slideUp("slow");
		}
		//$('#wedding-list div.wish-wed-list').toggle();
		return false;
	});
	
	$('#wedding-list div.wlist-content h5').click(function(){
		$(this).toggleClass('expanded').next().toggle();
	});

	$('#wedding-list div.wlist-content ul').hide();
	$('#wedding-list div.wlist-content h5:first').next().toggle();

	/* wishlist list */
	$('#wishlist h5.wishlist-list-title').click(function(){
		if ($('#wishlist div.wish-wed-list').is(":hidden")) {
			if ($('#wedding-list div.wish-wed-list').is(":show")) {
				$('#wedding-list div.wish-wed-list').slideUp("slow");
			}
			$('#wishlist div.wish-wed-list').slideDown("slow");
		}else{
			$('#wishlist div.wish-wed-list').slideUp("slow");
		}
		//$('#wishlist div.wish-wed-list').toggle();
		return false;
	});
	
	/* ADVICES */
	$('#user-login .advices-title a.prev,#user-login .advices-title a.next').click(function(){ return false;});
	$("#user-login .advices-content").scrollable({
	    size: 1,
	    clickable: false,
	    //globalNav: true,
	    items: '#user-login .advices_holder',
	    prevPage: '#user-login .prev',
	    nextPage: '#user-login .next',
	    loop: true,
	    speed: 400
	})/*.autoscroll(5000)*/; 
	
	/* dialog */
	$('div#pop-up-window').dialog({
		resizable: false,
		autoOpen: false,
		zIndex: 1000,
		modal: true,
		/*height: 450,
		width: 600,*/
		draggable: false,
		dialogClass: 'alert',
		closeOnEscape: true,
		closeText: 'hide',
		close: function(){$('div#pop-up-window').html('');}
	});
	
	/* video */
	$('a.video-preview-link').click(function(){
		
		content_url = video_preview_url + $(this).attr('value');
		
		var pop_up_window = $('div#pop-up-window');
		var flv_plaeyr_path = $('a#flv_plear_path').attr('href');
		
		$(pop_up_window).html($('div#pop-up-shel').html());
		$('div#loader',pop_up_window).show();
		$('a.close',pop_up_window).click(function(){
			$(pop_up_window).dialog('close');
			return false;
		});
	
		$(pop_up_window).dialog('option', 'height', 470);
		$(pop_up_window).dialog('option', 'width', 600);
		$(pop_up_window).dialog('open');
		
		$.ajax({
			type: "GET",
			url: content_url,
		 	success: function(html){
				$('div#loader',pop_up_window).hide();
				//$('div#pop-up-window').append(html);
				$('div#pop-up-window-conten',pop_up_window).html(html);
				/* VIDEO */
				if (typeof flowplayer == 'function') {
					flowplayer("a.flv-player", flv_plaeyr_path ,{
						clip : { 
							autoPlay: true, 
							autoBuffering: true
						} 
					}); 
				}
		  	}
		});
		
		return false;
	});
	
	/* LightBox */
	
	$('.lightbox[rel!="lightbox[gallery]"]').click(function(){
		var pop_up_window = $('div#pop-up-window');
		$(pop_up_window).html($('div#pop-up-shel').html());
		$('div#loader',pop_up_window).show();
		$('a.close',pop_up_window).click(function(){
			$(pop_up_window).dialog('close');
			return false;
		});
		$('div#loader',pop_up_window).hide();
		
		var content = $(this).next().html();
		$('div#pop-up-window-conten',pop_up_window).html(content);
		
		$(pop_up_window).dialog('option', 'minHeight', 50);
		$(pop_up_window).dialog('option', 'width', 'auto');
		$(pop_up_window).dialog('option', 'minWidth', 50);
		$(pop_up_window).dialog('open');
		
		return false;
	});
	

	var cLightboxCurrentImage = null;
	$('.lightbox[rel="lightbox[gallery]"]').click(function(){
		
		var pop_up_window = $('div#pop-up-window');
		$(pop_up_window).html($('div#pop-up-shel-pagination').html());
		$('div#loader',pop_up_window).show();
		$('a.prev',pop_up_window).click(function(){
			tmp_link = 'a#'+(parseInt($(cLightboxCurrentImage).attr('id'))-1);
			var content = $(tmp_link).next('.pop-up-content').html();
			if(content){
				$('div#pop-up-window-conten',pop_up_window).html(content);
				cLightboxCurrentImage = $(tmp_link);
			}else{
				tmp_link = "a.lightbox.img-thumb.img-thumb-clip:last";
				content = $(tmp_link).next('.pop-up-content').html();
				if(content){
					$('div#pop-up-window-conten',pop_up_window).html(content);
					cLightboxCurrentImage = $(tmp_link);
				}
			}
			return false;
		});
		$('a.next',pop_up_window).click(function(){
			tmp_link = 'a#'+(parseInt($(cLightboxCurrentImage).attr('id'))+1);
			var content = $(tmp_link).next('.pop-up-content').html();
			if(content){
				$('div#pop-up-window-conten',pop_up_window).html(content);
				cLightboxCurrentImage = $(tmp_link);
			}else{
				content = $("a#0").next('.pop-up-content').html();
				if(content){
					$('div#pop-up-window-conten',pop_up_window).html(content);
					cLightboxCurrentImage = $("a#0");
				}
			}
			return false;
		});
		$('a.close',pop_up_window).click(function(){
			$(pop_up_window).dialog('close');
			return false;
		});
		$('div#loader',pop_up_window).hide();
		
		cLightboxCurrentImage = $(this);
		var content = $(cLightboxCurrentImage).next().html();
		$('div#pop-up-window-conten',pop_up_window).html(content);
		
		$(pop_up_window).dialog('option', 'width', 450);
		
		$(pop_up_window).dialog('option', 'minHeight', 50);
		$(pop_up_window).dialog('option', 'minWidth', 50);
		
		
		$(pop_up_window).dialog('open');
		
		return false;
	});
	
	
	/* jqzoom - jqzoom.pack.1.0.1.js*/
	
	//$('.jqzoom').jqzoom($jqzoom_options);

	
	
	/* CAKE */
	$('table.cake-sql-log caption').click(function(){
		$('table.cake-sql-log thead,table.cake-sql-log tbody').toggle();		
	});
	
	
	/* FALOURITES */
	
	$('a.add-to-fav').click(function(){
		addToFavorite($(this).attr('href'),false);
//		alert('Uspeshno dobawihte produkt w predpo4itani!');
		return false;
	});

	$('a#add-all-to-fav').click(function(){
		addToFavorite($('a.add-to-fav.product').attr('href'),true);
		$('span.designer-advice-item-on-focus').each(function(){
			addToFavorite($(this).html(),true);
		});
		alert('Вие добавихте тези продукти в Предпочитани!');
		//alert('Uspeshno dobawihte produkt w predpo4itani!');

		return false;
	});

	$('div.designer-advice div.pagination a').click(function(){return false;});
	
	var api = $("div.designer-advice div.item-teaser").scrollable({
		size: 1,
	    clickable: false,
	    //globalNav: true,
	   // api: true,
	    items: 'div.designer-advice div.item-teaser div.item-teaser-holder',
	    prevPage: 'div.designer-advice div.pagination a.prev',
	    nextPage: 'div.designer-advice div.pagination a.next',
	    onSeek: function(){
			elem = this.getVisibleItems().get(0);
			var teaser = this.getRoot();
			var advice = $(teaser).parent(); 
			id = $('a.add-to-fav',elem).attr('href');
	    	$('span.designer-advice-item-on-focus',advice).html(id);
	    },
	    onBeforeSeek: function(elem, speed){
			elem = this.getVisibleItems().get(0);
			var teaser = this.getRoot();
			var advice = $(teaser).parent(); 

			var tmp_api= this;
			
			if(this.getPageIndex()+2>=this.getPageAmount()){
				var url = $('a.designer-advice-other_category_items_url',advice).attr('href')+'/offset:'+this.getPageIndex();
				$.ajax({
					type: "GET",
					url: url,
				 	success: function(html){
						var loader = $('.item-teaser-holder div.item-teaser-holder-item.loader',teaser);

				 		if(!html){
					 		var see_all = $('a.designer-advice-category-url',advice);
					 		see_all.css('display','block');
				 			$(loader).html(see_all);
				 			tmp_api.reload();
				 			return;
				 		}
				 		
						$('.item-teaser-holder',teaser).append($(loader).clone());
						tmp_api.reload();
						$(loader).removeClass('loader');
						$(loader).html(html);
						
						$('a.quick-view').click(function(){
							quickViewWindow(this);
							return false;
						});
						
						$('a.add-to-fav:last',teaser).click(function(){
							addToFavorite($(this).attr('href'),false);
							//alert('Uspeshno dobawihte produkt w predpo4itani!');
							return false;
						});
				  	}
				});
			}
			
		}
	});

	$('a.load-favourite-items').click(function(){
		var pop_up_window = $('div#pop-up-window');
		$(pop_up_window).html($('div#pop-up-shel').html());
		$('a.close',pop_up_window).click(function(){
			$(pop_up_window).dialog('close');
			//window.location.reload();
			return false;
		});
		$('div#loader',pop_up_window).hide();
		//$('div#pop-up-window').append($('#load-favourite-items').html());
		$('div#pop-up-window-conten',pop_up_window).html($('#load-favourite-items').html());

		$("#VisitorReadForm",pop_up_window).submit(function(){
			form_url = $("#VisitorReadForm",pop_up_window).attr('action');
			$.post(
				form_url, 
				$("#VisitorReadForm",pop_up_window).serialize(),
				function (data, textStatus) {
					if(textStatus == 'success' && data=='true'){
						$('#VisitorReadForm .popup-content',pop_up_window).html('Данните са заредени');
						window.location.reload();
					}
				}
			);
			return false;
		});

		//$('div#pop-up-window').dialog({height:95,width:330});
		$(pop_up_window).dialog('option', 'height', 95);
		$(pop_up_window).dialog('option', 'width', 330);
		$(pop_up_window).dialog('open');
		return false;
	});

	$('a.preserve-favourite-items').click(function(){
		var pop_up_window = $('div#pop-up-window');
		$(pop_up_window).html($('div#pop-up-shel').html());
		$('a.close',pop_up_window).click(function(){
			$(pop_up_window).dialog('close');
			return false;
		});
		$('div#loader',pop_up_window).hide();
		//$('div#pop-up-window').append($('#preserve-favourite-items').html());
		$('div#pop-up-window-conten',pop_up_window).html($('#preserve-favourite-items').html());

		$("#VisitorAddForm",pop_up_window).submit(function(){
			form_url = $("#VisitorAddForm",pop_up_window).attr('action');
			$.post(
				form_url, 
				$("#VisitorAddForm",pop_up_window).serialize(),
				function (data, textStatus) {
					if(textStatus == 'success' && data=='true'){
						$('#VisitorAddForm .popup-content',pop_up_window).html('Даните са записани');
					}
				}
			);
			return false;
		});

		//$('div#pop-up-window').dialog({height:95,width:330});
		$(pop_up_window).dialog('option', 'height', 95);
		$(pop_up_window).dialog('option', 'width', 330);
		$(pop_up_window).dialog('open');
		return false;
	});
	
	$('a.send-favourite-items').click(function(){
		var pop_up_window = $('div#pop-up-window');
		$(pop_up_window).html($('div#pop-up-shel').html());
		$('a.close',pop_up_window).click(function(){
			$(pop_up_window).dialog('close');
			//window.location.reload();
			return false;
		});
		$('div#loader',pop_up_window).hide();
		$('div#pop-up-window-conten',pop_up_window).html($('#send-favourite-items').html());

		$("#FavouriteSendToEmailForm",pop_up_window).submit(function(){
			form_url = $("#FavouriteSendToEmailForm",pop_up_window).attr('action');
			$.post(
				form_url, 
				$("#FavouriteSendToEmailForm",pop_up_window).serialize(),
				function (data, textStatus) {
					if(textStatus == 'success' && data=='true'){
						$('#FavouriteSendToEmailForm .popup-content',pop_up_window).html('Email-а е изпратен');
					}else if(textStatus == 'success'){
						$('#FavouriteSendToEmailForm .popup-content',pop_up_window).html(data);
					}
				}
			);
			return false;
		});

		//$('div#pop-up-window').dialog({height:95,width:330});
		$(pop_up_window).dialog('option', 'height', 95);
		$(pop_up_window).dialog('option', 'width', 330);
		$(pop_up_window).dialog('open');
		return false;
	});
	
	$('div.wish-wed-list div.item-teaser a.rem-from-fav').click(function(){
		removeFromFavorite($(this).attr('href'));
		val = $(this).parent().parent().attr('value');
		$('div.item-teaser[value='+val+']').remove();
		//alert('Removed item form favourites');
		return false;
	});


	var api_wish_list = $("div.wish-wed-list div.wlist-content").scrollable({
		size: 5,
	    clickable: false,
	    items: 'div.wish-wed-list div.wlist-content div.wlist-content-holder',
	    item: 'div.item-teaser', 
	    prevPage: 'div.wish-wed-list div.pagination a.prev',
	    nextPage: 'div.wish-wed-list div.pagination a.next'
	});
	
	$('a.quick-view').click(function(){
		quickViewWindow(this);
		return false;
	});
	
	/* PRODUCT FILTERS */
	$('#filters div.filter span.filter-title').click(function(){
		$(this).next().toggle();
		return false;
	});
	$('#filters div.filter').hover(null,function(){
		$('ul.filter-list',this).hide();
	});
	
	
	
	/* HOME PAGE */
$('div#home-services div.pagination a,div#home-accessories div.pagination a,div#home-products div.pagination a').click(function(){return false;});
	
	$("div#home-services div.slideshow-imgs").scrollable({
		size: 1,
	    clickable: false,
	    items: 'div#home-services div.slideshow-imgs div.slideshow-imgs-holder',
	    prevPage: 'div#home-services div.pagination a.prev',
	    nextPage: 'div#home-services div.pagination a.next',
	    item: 'div.item-teaser-holder',
	    onBeforeSeek: function(elem, speed){
			elem = this.getVisibleItems().get(0);
			var teaser = this.getRoot();
			var advice = $(teaser).parent(); 
			
			var tmp_api= this;
			if(this.getPageIndex()+2 == this.getPageAmount()){
				var url = $('a.other_category_items_url',advice).attr('href')+'/offset:'+(this.getPageIndex()+1);
				$.ajax({
					type: "GET",
					url: url,
				 	success: function(html){
						var loader = $('.slideshow-imgs-holder div.item-teaser-holder.loader',teaser);
	
				 		if(!html){
					 		var see_all = $('a.category-url',advice);
					 		see_all.css('display','block');
				 			$(loader).html(see_all);
				 			tmp_api.reload();
				 			return;
				 		}
				 		
						$('.slideshow-imgs-holder',teaser).append($(loader).clone());
						tmp_api.reload();
						$(loader).removeClass('loader');
						$(loader).html(html);
				  	}
				});
			}
		}
	});

	$("div#home-accessories div.slideshow-imgs").scrollable({
		size: 1,
	    clickable: false,
	    items: 'div#home-accessories div.slideshow-imgs div.slideshow-imgs-holder',
	    prevPage: 'div#home-accessories div.pagination a.prev',
	    nextPage: 'div#home-accessories div.pagination a.next',
	    item: 'div.item-teaser-holder',
	    onBeforeSeek: function(elem, speed){
			elem = this.getVisibleItems().get(0);
			var teaser = this.getRoot();
			var advice = $(teaser).parent(); 
	
			var tmp_api= this;
			if(this.getPageIndex()+2>=this.getPageAmount()){
				var url = $('a.other_category_items_url',advice).attr('href')+'/offset:'+(this.getPageIndex()+1);
				$.ajax({
					type: "GET",
					url: url,
				 	success: function(html){
						var loader = $('.slideshow-imgs-holder div.item-teaser-holder.loader',teaser);
	
				 		if(!html){
					 		var see_all = $('a.category-url',advice);
					 		see_all.css('display','block');
				 			$(loader).html(see_all);
				 			tmp_api.reload();
				 			return;
				 		}
				 		
						$('.slideshow-imgs-holder',teaser).append($(loader).clone());
						tmp_api.reload();
						$(loader).removeClass('loader');
						$(loader).html(html);
				  	}
				});
			}
		}
	});

	$("div#home-products div.slideshow-imgs").scrollable({
		size: 1,
	    clickable: false,
	    items: 'div#home-products div.slideshow-imgs div.slideshow-imgs-holder',
	    prevPage: 'div#home-products div.pagination a.prev',
	    nextPage: 'div#home-products div.pagination a.next',
	    item: 'div.item-teaser-holder',
	    onBeforeSeek: function(elem, speed){
			elem = this.getVisibleItems().get(0);
			var teaser = this.getRoot();
			var advice = $(teaser).parent(); 
	
			var tmp_api= this;
			if(this.getPageIndex()+2>=this.getPageAmount()){
				var url = $('a.other_category_items_url',advice).attr('href')+'/offset:'+(this.getPageIndex()+1);
				$.ajax({
					type: "GET",
					url: url,
				 	success: function(html){
						var loader = $('.slideshow-imgs-holder div.item-teaser-holder.loader',teaser);
	
				 		if(!html){
					 		var see_all = $('a.category-url',advice);
					 		see_all.css('display','block');
				 			$(loader).html(see_all);
				 			tmp_api.reload();
				 			return;
				 		}
				 		
						$('.slideshow-imgs-holder',teaser).append($(loader).clone());
						tmp_api.reload();
						$(loader).removeClass('loader');
						$(loader).html(html);
				  	}
				});
			}
		}
	});
	
	$('div.header-img.home div.headimage a.prev,div.header-img.home div.headimage a.next').click(function(){return false;});
	var home_slide_show = $("div.header-img.home div.headimage").scrollable({
		size: 4,
	    clickable: false,
	    api:true,
	    items: 'div.slideshow-imgs',
	    prev: '.prev',
	    next: '.next',
	   	item: 'div.item-holder',
	   	onSeek: function(elem, index){
			//parseInt
			$('ul.tags li.tags-lable a').each(function(){
				a_index = $(this).attr('href');
				if( index >= parseInt(a_index)){
					$('ul.tags li.tags-lable.selected').removeClass('selected');
					$(this).parent().addClass('selected');
				}
			});
		}
	});
	$('ul.tags li.tags-lable a').click(function(){
		new_index = $(this).attr('href');
		home_slide_show.seekTo(new_index);
		return false;
	});
	
	/* PRODUCT GALLERY */
	$('div.gallery div.pagination a').click(function(){return false;});
	$("div.gallery-content").scrollable({
		size: 6,
	    clickable: false,
	    //globalNav: true,
	   // api: true,
	    items: 'div.gallery-content div.gallery-content-holder',
	    item: 'a.img-thumb',
	    prevPage: 'div.gallery div.pagination div.wrapper a.prev',
	    nextPage: 'div.gallery div.pagination div.wrapper a.next'
	});
	
});
