var products = new Array();
var currentIndex = 0;
$(document).ready( function() {

	$(".share_btn").mouseover( function() {
		$(this).addClass("ui-state-hover");
	});
	
	$(".share_btn").mouseout( function() {
		$(this).removeClass("ui-state-hover");
	});
	
	$(".submit_btn").mouseover( function() {
		$(this).addClass("ui-state-hover");
	});
	
	$(".submit_btn").mouseout( function() {
		$(this).removeClass("ui-state-hover");
	});
	
	$(".submit_btn").click( function(e) {
		$(this).parents("form").submit();
	});

	$("#nav li span").each( function() {
		var data = $(this).metadata();
		$(this).css("background-image", "url(" + data.picture + ")");
	});
	
	$("#social li a").each( function() {
		var data = $(this).metadata();
		$(this).css("background-image", "url(" + data.picture + ")");
	});
	
	$("#nav li a").hover( 
		function() {
			$(this).children("span").animate( { top: -22 }, 100 );
		}, 
		function () {
			$(this).children("span").animate( { top: -20 }, 100 );
		}
	);
	
	$("#posts_filter li").hover( 
		function() {
			$(this).addClass('hover');
		}, 
		function () {
			$(this).removeClass('hover');
		}
	);
	
/*	$(".calendar").datepicker({
		dateFormat: 'yymmdd',
		beforeShowDay: function (date) {
			var todayDate = new Date();
			var myclass = '';
			var selectable = false;
			*//*

			var presets = [ '20100616', '20100605' ];

			var myFullYear = date.getFullYear().toString();
			var myMonth = (date.getMonth() + 1).toString();
			if (myMonth.length==1) myMonth = '0' + myMonth;
			var myDay = date.getDate().toString();
			if (myDay.length==1) myDay = '0' + myDay;
			var formattedDate = myFullYear + myMonth + myDay;

			if ( $.inArray( formattedDate, presets ) >= 0 ) {
				myclass = 'datepicker_event';
			} else {
				myclass = '';
			}*//*

			if (date <= todayDate) {
				selectable = true;
			}

			return [selectable, myclass];
		},
		onSelect: function (date) {
			window.location = 'http://www.pigeonsaspets.co.uk/?m=' + date;
		}
	});*/
	
	$("#filter_content div.item").hide();
	$("#posts_filter li").click( function() {
		var obj = $(this);
		
		var data = $(this).metadata();
		if (!$(this).hasClass('selected') && $("#filter_content").is(":visible")) {
			$("#posts_filter li").each( function() {
				$(this).removeClass('selected');
			})
			$("#filter_content div.item:visible").fadeOut('fast', function() {
				$("div." + data.myFilter).fadeIn();
			});
			$(this).addClass('selected');
		} else {
			if ( $('#filter_content').not(":visible") ) {
				$("div." + data.myFilter).show();	
			}
			$(this).addClass('selected');
			$("#filter_content").slideToggle('fast', function() {
				if ( $("#filter_content").is(":hidden") ) {
					$("#filter_content div.item").hide();
					$("#posts_filter li").each( function() {
						$(this).removeClass('selected');
					})
				} else {
					$("div." + data.myFilter).fadeIn();	
					$(obj).addClass('selected');
				}
			});
		}
	});
	
	$("#social li a").hover( 
		function() {
			$(this).animate( { top: -16 }, 200 );
		},
		function () {
			$(this).animate( { top: 0 }, 200 );
		}
	);
	
	$(".mo-glow").hover( 
		function() {
			$(this).addClass('glow');
		},
		function () {
			$(this).removeClass('glow');
		}
	);
	
	$(".post_date").each( function() {
		var data = $(this).metadata();
		$(this).find(".post_day").css("background-position", "-" + ((data.day-1)*80) + "px 0px");
		$(this).find(".post_month").css("background-position", "-" + ((data.month-1)*80) + "px 0px");
		$(this).find(".post_year").css("background-position", "-" + ((data.year-2010)*80) + "px 0px");
	});
	
	$.jTwitter('pigeonsaspets', function(posts) {
		$(posts).each( function(index) {
			$("#twitter_col_content .loader").hide();
			$("#twitter_col_content ul").append('<li><b>' + posts[index].user.name + '</b>&nbsp;' + replaceURLWithHTMLLinks(posts[index].text) + '</li>');
		});
	});
	
	var flickr_url = "http://api.flickr.com/services/rest/?&method=flickr.people.getPublicPhotos&api_key=011a8fd74d05263085492e65ca572f04&user_id=46599840@N08&format=json&per_page=20&jsoncallback=?";
	$.getJSON(
		flickr_url, 
		function(data) { 
			$.each(data.photos.photo, function(i, item) {
				var photoURL_s = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_s.jpg';
				var photoURL_b = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '_d.jpg';
				$("#flickr_thumbs").append('<a href="' + photoURL_b + '" class="flickr_lightbox"><img src="' + photoURL_s + '" height="38" width="38" alt="' + item.title + '" /></a>');
				$("#flickr_thumbs img").css('opacity', 0.5);
				$(".flickr_lightbox").lightBox();
				$("#flickr_content .loader").hide();
			});
		}
	);
	
	var myVideos = [];
	var numVideos = 2;
	$.jTube({
		user: 'PigeonsAsPets', 
		limit: numVideos, 
		page: 1, 
		success: function(videos) {
			$(videos).each( function(i, video) {
				$("#youtube_col_content").append( $.jTubeEmbed(video.video, {width: 218, height: 160}) );
			});
			$("#youtube_col_content .loader").hide();
		}
	});
	
	$("#flickr_thumbs img").live("mouseover", function() {
		$(this).fadeTo('fast', '1');
	});
	$("#flickr_thumbs img").live("mouseout", function() {
		$(this).fadeTo('fast', '0.5');
	});
	
	
	$("#feedback_tab").hover( 
		function() {
			$(this).animate( { right: 0 }, 100 );
		},
		function () {
			$(this).animate( { right: -5  }, 100 );
		}
	);
	
	$("#feedback_modal").dialog({
		autoOpen: false, 
		height: 600, 
		width: 470, 
		modal: true, 
		resizable: false, 
		title: 'Feedback', 
		buttons: {
			'Send': function() {
				var myFullname = $("#feedback_modal input[name='fullname']").val();
				var myWhere = $("#feedback_modal select[name='country'] option:selected").text();
				var myEmail = $("#feedback_modal input[name='email']").val();
				var myFeedback = $("#feedback_modal textarea[name='feedback']").val();
				
				if (myFullname=='' || myEmail=='' || myFeedback=='') {
					alert("Please fill in all the fields");
				} else {
					$.ajax({
						url: 'wp-feedback.php',
						data: ({ fullname: myFullname, where: myWhere, email: myEmail, feedback: myFeedback }), 
						success: function () {
							$("#feedback_modal").dialog('close');
							$("#feedback_modal input[name='fullname']").val("");
							$("#feedback_modal input[name='email']").val("");
							$("#feedback_modal textarea[name='feedback']").val("");
							alert("Thank you for your feedback!");
						}
					});
				};
			}, 
			Cancel: function() {
				$(this).dialog('close');
			}
		}, 
		close: function() {
			$("#feedback_modal input").val("");
			$("#feedback_modal select").val("");
			$("#feedback_modal textarea").val("");
		}
	});
	
	$("#feedback_tab").click( function(e) {
		e.preventDefault();
		$("#feedback_modal").dialog('open');
	});
	
	$(".input_highlight").focus( 
		function() {
			$(this).css("background-position", "0px -36px");
		}
	);
	
	$(".input_highlight").blur( 
		function() {
			$(this).css("background-position", "0px 0px");
		}
	);
	
	$(".textarea_highlight").focus( 
		function() {
			$(this).css("background-position", "0px -233px");
		}
	);
	
	$(".textarea_highlight").blur( 
		function() {
			$(this).css("background-position", "0px 0px");
		}
	);
	$(".share_sn_icons").fadeOut();
	
	$(".share_btn").click( function() {
		$(this).next(".share_sn_icons").show();	
		$(this).next(".share_sn_icons").css('opacity', 1);
		var data = $(this).metadata();
	});
	$(".share_sn_icons").hover( 
		function () {
			$(this).stop();
			$(this).animate({ opacity: 1 });
		}, 
		function () {
			$(this).animate({ opacity: 0 }, function() {
				$(this).hide();	
			});
		}
	);
	
	$("#column").sortable({
		items: '.moveable', 
		containment: '#column', 
		placeholder: 'moveable_placeholder',
		axis: 'y', 
		opacity: 0.6
	});
	$("#column").disableSelection();

	var SlideDelay = 12;
	$.jGFeed('http://pigeonsaspets.spreadshirt.co.uk/shop/feed', function(feed) {
		$(feed.entries).each( function (i, item) {
			var product = new Array();
			
			product['title'] = item.title;
			product['link'] = item.link;
			product['content'] = item.content;
			var contentObject = item.content;
			
			products.push(product);
			$("#paps_shop_content .loader").hide();
		});
		$("#paps_shop_content_title").html('<a href="' + products[0]['link'] + '">' + products[0]['title'] + '</a>');
		$("#paps_shop_content_image").html(products[0]['content']);
		setInterval("rotateProducts()", (SlideDelay * 1000));
	}, 1000);
	
});
function rotateProducts() {

	$("#paps_shop_content_title").fadeOut( function() {
		$("#paps_shop_content_title").html('<a href="' + products[currentIndex]['link'] + '">' + products[currentIndex]['title'] + '</a>');
		$("#paps_shop_content_title").fadeIn();
	});
	$("#paps_shop_content_image").fadeOut( function() {
		$("#paps_shop_content_image").html(products[currentIndex]['content']);
		$("#paps_shop_content_image").fadeIn();
	});
	
	currentIndex++;
	if (currentIndex > (products.length -1)) {
		currentIndex = 0;
	}
};

function replaceURLWithHTMLLinks(text) {
	var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
	return text.replace(exp,"<a href='$1'>$1</a>"); 
}
function videoSort( a, b ) {
	if (a.published > b.published) {
		return 0;
	} else {
		return 1;
	}
}
