/**
* Flickr photo feed
*
* A script to add flickr images on my own website and
* with integrated animation effects. 
*
* Author	: Kim Sandvold
* Copyright	: Kim Sandvold
* Url		: http://www.kimsandvold.com
* E-mail	: mail@kimsandvold.com
* Created	: 07.10.2009
* Updated	: 17.10.2009
*/

var maxThumbSize = 130;
var myKey;

/* jQuery init */
$(document).ready(function(){

	$('body').append('<div id="decor_left"></div>');
	$('body').append('<div id="decor_right"></div>');

	$('a[rel*=contact]').click(function(){
		tcf($(this).attr('href'));
		return false;
	});

	/* Creating custom rss url */
	myKey = "45848381@N06";
	rssStr = "http://api.flickr.com/services/feeds/photos_public.gne?id="+myKey+"&lang=en-us&format=json&jsoncallback=?";

	$.getJSON(rssStr, function(data){
		
		$.each(data.items, function(i,item){
			
			/* Integrate the fancybox viewer plugin */
			$('a[class*=imgItem]').fancybox();
			
			/* Finding the width and height of each image */
			var img = new Image();
			img.src = item.media.m;			
			/* Inserts blank space if there is no title data - because the use of padding in stead of height in css */
			imgTitle = (item.title) ? item.title : '&nbsp;'; 
			
			/* Append the img tag and other to the website */
			$("<img/>").
				attr("src", item.media.m.replace(/_m/, "_s")).
				attr("class", "img").
				attr("alt", item.title).
				appendTo("#imgCont").
				wrap("<div class='thumb'><div class='thumb-inner'><a class='imgItem' rel='group' title='Nr. "+imgTitle+" - &copy; pulsvarmere.no' href='"+item.media.m.replace(/_m/, "")+"'></a></div><div class='desc'>Nr. "+imgTitle+"</div></div>");
		});

	});
	
	/* Animated hash slide */
	$('a[href*=#]').click(function() {
		if(location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 300);
				return false;
			}
		}
	});
});

/* Closeing the contact form */
function closeWin(){

	$('#contactForm').remove();
}

/* Sending the email */
function sendData(){

	var urlStr = 'process.php?n='+ $('#i_name').val() +'&e='+ $('#i_email').val() +'&m='+$('#contactMessage').val() +'';
	
	$.ajax({
		type: "GET",
		url: urlStr,
		data: "",
		success: function(msg){
			if(msg == ''){
				$('#center').html('<div id="heading">E-mail sendt!</div><p><a href="#" onclick="closeWin();return false;">Tilbake til hjemmesiden</a></p>');
			}else{
				$('#center').html('<div id="heading">Feil!</div><p><a href="#" onclick="closeWin();return false;">'+msg+'. Tilbake til hjemmesiden</a></p>')
			}
		}
	});
}

/* Loading the contact form */
function tcf(data){

	$.ajax({
		type: "POST",
		url: data,
		data: "",
		success: function(msg){
			$("body").append(msg);
			$('#i_name').focus();
		}
	});
}

/* Alert to confirm rss subscription */
function subscribe(){

	var answer = confirm("Ved å akseptere denne meldingen vil du abonnere på RSS Feeds fra Solfrids flickr-side");
	if (answer){
		window.location = "http://api.flickr.com/services/feeds/photos_public.gne?id="+myKey+"&lang=en-us&format=rss_200";
	}
	else{return false;}
}

/* Facebook sharing popup */
function fbs_click() {

	var width = 626;
	var height = 400;
	var pop_top = (window.innerHeight / 2) - (height / 2);// Centers the popup vertically
	var pop_left = (window.innerWidth / 2) - (width / 2);// Centers the popup horizontally 
	u = location.href;
	t = document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width='+width+',height='+height+', left='+pop_left+',top='+pop_top+'');
	return false;
}
