
$(document).ready(function(){

	//Hide more then 10 tags
	$("#skwr_addtags ul li:gt(10)").hide();
//	$(".wp-tag-cloud li:gt(10)").hide();


	//Welcome Screen
	//if ($.cookie('welcome') != 'seen_it') {
		$('#welcome').attr("id","welcome-active");
		$('#welcome-active span.decoration').addClass('welcome-decoration');
		//$('#welcome-active span.close').replaceWith('<a href="#close" class="close">Aloita tästä</a>');

		/*$('#welcome-active a.close').click(function(){
			$('#welcome-active').fadeOut();
		});*/
	//}

	//Popup Screen
	$('.close, .close-button').live('click', function() {
		$(this).parent().hide();
	});



	//Toggle Tag-list
	var Toggled=false;
	$('.view-all').click(function(){
		if(Toggled==false){$('.view-all').html('Piilota avainsanat'); Toggled=true;}
		else{$('.view-all').html('Näytä kaikki');Toggled=false;}
		$("#skwr_addtags ul li:gt(10)").toggle();
		return false;
	});



	var date = new Date();
	date.setTime(date.getTime() + (365 * 24 * 60 * 60 * 1000));
	var options = { path: '/', expires: date, domain: 'www.lahjaekstra.fi' };
	$.cookie('welcome', 'seen_it', options);

	$('.grid').click(function(){
		$('.view-view').hide();
		$(this).addClass('current');
		$('.list').removeClass('current');
		$('body').removeClass('list-view');
		$('.view-view').fadeIn('slow');
		$('body').addClass('grid-view');
	    //$.cookie('GridView', 'grid-view')
		$.cookie('ListView', null);
		//return false;
	});

	//List view
	$('.list').click(function(){
		$('.view-view').hide();
		$(this).addClass('current');
		$('.grid').removeClass('current');
		$('body').removeClass('grid-view');
		$('.view-view').fadeIn('slow');
		$('body').addClass('list-view');
        $.cookie('ListView', 'list-view');
		//$.cookie('GridView', null);
		//return false;
	});
	
//	alert($.cookie('ListView'));

    if ($.cookie('ListView') == 'list-view') {
		$.cookie('GridView', null);
		$('dd.list').addClass('current');
		$('dd.grid').removeClass('current');
		$('body').removeClass('grid-view');
		$('body').addClass('list-view');
//		alert('LIST');	
    } else {
		$('dd.grid').addClass('current');
		$('dd.list').removeClass('current');
		$('body').removeClass('list-view');	
		$('body').addClass('grid-view');
//		alert('GRID') 
	
	};

	//Carousel
    $("#carousel").jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
		visible: 1,
		start: 0		//afterEnd: function( a ) {
		//	alert("After animation ends:" + a.html());
		//	}
	    });

	$('.carousel').click(function() {
		var index = $(this).attr('id').substring($(this).attr('id').indexOf('_')+1);
		$.cookie('carousel', index, options);
	});

	//Open external links in a new window (using rel="external")
	$('a[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	}); 


	$('.grid-view .product .introduction a').click(function(){
		$('.grid-view .product').removeClass('product-open');
		$(this).parent().parent().parent().addClass('product-open');
		return false;

		$('.grid-view .close').click(function(){
			$('.grid-view .product').removeClass('product-open');
			return false;
		});

	});

	$('.thumbnail').hover(function(){
		if ($('body').attr('class').indexOf('grid-view') != -1) {
			$(this).children('.introduction').fadeIn('fast');
		}
	},
	function(){
		if ($('body').attr('class').indexOf('grid-view') != -1) {
			$(this).children('.introduction').hide();
		}
	});


	//POPUP
	$('.subscribe').popupWindow({ 
		height:600,
		width:570,
		scrollbars:1,
		centerBrowser:1
		});
	$('.rules').popupWindow({ 
		height:600,
		width:570,
		scrollbars:1,
		centerBrowser:1
		});
	$('.share-facebook').popupWindow({ 
		height:400,
		width:570,
		scrollbars:0,
		centerBrowser:1
		});

	$("#contest").validate( {
		rules: {
			first_name: "required",
			last_name: "required",
			address: "required",
			city: "required",
			postcode: "required",
			date_of_birth: "required",
			kilpailuehtoihin: "required",
			cell: "required",
			email: {
				required: true,
				email: true
			}
		},
		messages: {
			first_name: "Etunimi puuttuu",
			last_name: "Sukunimi puuttuu",
			address: "Osoite puuttuu",
			city: "Kaupunki puuttuu",
			postcode: "Anna oikea postinumero",
			date_of_birth: "Täytä syntymäaikasi",
			email: "Sähköpostiosoite on väärä tai puuttuu",
			cell: "Puhelinnumero on väärä tai puuttuu",
			kilpailuehtoihin: "Sinun täytyy hyväksyä kilpailuehdot"
		}
	});
	
	//Validates the Newsletter form
	$("#newsletter").validate({
		rules: {
		email: {
				required: true,
				email: true
			}
		},
		messages: {
			email: "Sähköpostiosoite on väärä tai puuttuu"
		},
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					target: "#result"
				});
			}
	});



	$('#email').clearField();
	$('#message_body').clearField();

	if ($.cookie('selected_items') && $.cookie('selected_list')) {

		$('#wishlistContent').html($.cookie('selected_items'));
		$('.print').find('a').attr('href', 'http://www.lahjaekstra.fi/wishlist/?list=' + $.cookie('selected_list'));
		$('.facebook').find('a').attr('href', 'http://www.facebook.com/sharer.php?u=http://www.lahjaekstra.fi/wishlist/?list=' + $.cookie('selected_list'));
	}
	
	if ($.cookie('width')) {
		$('.wishlistContent').css('width', $.cookie('width'));
	}
	
	$('.draggable').mousedown(function(){
		$('.introduction').hide();
		$('#wishlistBar').addClass('focus');
	});
	
	$('body').mouseup(function(){
		$('#wishlistBar').removeClass('focus');
	});

	$('.draggable').draggable({
		helper: 'clone',
		revert: true,
		revertDuration: 1
	});
	
	$('#wishlistBar').droppable({
		 accept: ".draggable",
		 drop: function(ev, ui) {
		 	var width = parseInt($('.wishlistContent').css('width').replace('px', '')) + 80;
			width += 'px';
			$.cookie('width', width, options);
		 	$('.wishlistContent').css('width', width);
			$('#pane1').jScrollHorizontalPane();
			var id = ui.draggable.attr('id').substring(ui.draggable.attr('id').indexOf('_')+1);
		 	var content = '<li class="wish-item" id="wish-item_' + id + '">' + ui.draggable.html() + '<span>Poista tuote</span></li>';
			$('.empty').html('');
			$('#wishlistContent').append(content);
			$.cookie('selected_items', $('#wishlistContent').html(), options);
			
			var items = $('#wishlistContent').children('.wish-item');
			var list='';
			for (i=0; i<items.length; i++) {
				if (i == items.length-1) {
					list += $(items[i]).attr('id').substring($(items[i]).attr('id').indexOf('_')+1);
				} else {
					list += $(items[i]).attr('id').substring($(items[i]).attr('id').indexOf('_')+1) + ',';
				}
			}
			$('.print').find('a').attr('href', 'http://www.lahjaekstra.fi/wishlist/?list=' + list);
			$('.facebook').find('a').attr('href', 'http://www.facebook.com/sharer.php?u=http://www.lahjaekstra.fi/wishlist/?list=' + list);
			$.cookie('selected_list', list, options);
			
		 }
	});
	
	$('.wish-item').live('click', function(){
		$(this).remove();
		$.cookie('selected_items', $('#wishlistContent').html(), options);
		
		var items = $('#wishlistContent').children('.wish-item');
		var list='';
		for (i=0; i<items.length; i++) {
			if (i == items.length-1) {
				list += $(items[i]).attr('id').substring($(items[i]).attr('id').indexOf('_')+1);
			} else {
				list += $(items[i]).attr('id').substring($(items[i]).attr('id').indexOf('_')+1) + ',';
			}
		}
		$('.print').find('a').attr('href', 'http://www.lahjaekstra.fi/wishlist/?list=' + list);
		$('.facebook').find('a').attr('href', 'http://www.facebook.com/sharer.php?u=http://www.lahjaekstra.fi/wishlist/?list=' + list);
		$.cookie('selected_list', list, options);
		
		var width = parseInt($('.wishlistContent').css('width').replace('px', '')) - 80;
		if (width < 100) {
			width = 100;
		}
		width += 'px';
		$.cookie('width', width, options);
		$('.wishlistContent').css('width', $.cookie('width'));
		$('#pane1').jScrollHorizontalPane();
	});
	
	$('.share-list').find('.share').click(function(){
		var width = parseInt($('.wishlistContent').css('width').replace('px', '')) + 80;
		width += 'px';
		$.cookie('width', width, options);
		$('.wishlistContent').css('width', $.cookie('width'));
		$('#pane1').jScrollHorizontalPane();
		
		var id = $(this).parent().parent().parent().parent().find('.draggable').attr('id').substring($(this).parent().parent().parent().parent().find('.draggable').attr('id').indexOf('_')+1);
		var content = $(this).parent().parent().parent().parent().find('.draggable').html();
		content = '<li class="wish-item" id="wish-item_' + id + '">' + content + '<span>Poista tuote</span></li>';
		$('.empty').html('');
		$('#wishlistContent').append(content);
		$.cookie('selected_items', $('#wishlistContent').html(), options);
		
		var items = $('#wishlistContent').children('.wish-item');
		var list='';
		for (i=0; i<items.length; i++) {
			if (i == items.length-1) {
				list += $(items[i]).attr('id').substring($(items[i]).attr('id').indexOf('_')+1);
			} else {
				list += $(items[i]).attr('id').substring($(items[i]).attr('id').indexOf('_')+1) + ',';
			}
		}
		$('.print').find('a').attr('href', 'http://www.lahjaekstra.fi/wishlist/?list=' + list);
		$('.facebook').find('a').attr('href', 'http://www.facebook.com/sharer.php?u=http://www.lahjaekstra.fi/wishlist/?list=' + list);
		$.cookie('selected_list', list, options);
		
		return false;
	});
	
	$('.category_link').each(function(){
		var cat_link = $(this).attr('href').replace('/categories/', '/');
		$(this).attr('href', cat_link);
	});
	
	$('.category_link').click(function(){
		var index = $('.category_link').index(this);
		$.cookie('carousel', index, options);
	});


});

