/**
 *  Imageloader
 *  
 *  Parameter: 
 *  
 *  Container = The div container which is the 
 *  			parent container of the image to load
 * 	image = 	The image container
 * 	imageUrl = 	The url of the image
 * 
 * 	Usage:
 * 
 * 	1. Init imageLoader before storing Images in it
 * 	2. storeImages in it
 * 	3. Load images after succesfully loaded the page
 */

function ImageWrapper(container, image, imageUrl) {

	this.image = image;
	this.imageUrl = imageUrl;
	this.container = container;
}

var imageLoaderFunction = {
	resizeToWidth: 191,
	imageWrapperArray : Array,

	init : function() {

		this.imageWrapperArray = new Array();
	},

	storeImage : function(container, image, imageUrl) {

		this.imageWrapperArray.push(new ImageWrapper(container, image, imageUrl));

	},

	loadImages : function() {

		for ( var i = 0; i < this.imageWrapperArray.length; i++) {

			this.imageWrapperArray[i].image.attr('src', this.imageWrapperArray[i].imageUrl);
			this.imageWrapperArray[i].image.width(this.imageWrapperArray[i].container.width());
			//this.imageWrapperArray[i].image.height(this.imageWrapperArray[i].container.height());
		}

	},
	
	lazyLoad : function(targetSelector){
		$j(targetSelector).each(function(){
			
			$j(this).attr('src', $j(this).attr('longdesc')); 
			
		});
	},

	resize : function(targetClass) {
		$j(targetClass).each(function() {
			// Hole Bild in item_region
			// Hole alles was zwischen width und px ist
			var styleWidth = $j(this).find('.center_class').attr('style');
			var imageWidth = $j(this).find('.center_class').width();
			var imageHeight = $j(this).find('.center_class').height();
			if(styleWidth){
				styleWidth = styleWidth.match(/[^-]width\s*:\s*\d+/);
				if(styleWidth){
					styleWidth = styleWidth[0].match(/\d+/);
					if(styleWidth){
						imageWidth = styleWidth[0];
					}
				}
			}
			// Skalierung nötig
			if (imageWidth > imageLoaderFunction.resizeToWidth) {
				var ratio = imageHeight / imageWidth;
				$j(this).find('.center_class').width(imageLoaderFunction.resizeToWidth);

				var distance = imageWidth - imageLoaderFunction.resizeToWidth;
				imageHeight = imageHeight - (distance * ratio);

				$j(this).find('.center_class').height(imageHeight);
			} else {
				// Nicht skalieren, aber Bild zentrieren
				var amazonWidth = $j(this).find('.center_class').find('img').width();
				if(amazonWidth > 0 && amazonWidth < imageWidth){
					imageWidth = amazonWidth;
				}
				var margin_left = Math.round((imageLoaderFunction.resizeToWidth - imageWidth) / 2);
				$j(this).find('.center_class').css('margin-left', margin_left);
			}
		});
	},
	
	interactionPos: function(targetClass, top){
		$j(targetClass).each(function() {
			var image = $j(this).find('.modelImage');
	
			var height = Math.round((image.height() / 2) - 27);
			
			if(top){
				height = Math.round(0 - 27);
			}
			
			$j(this).find('.item_interaction').css('top', height);
		});
	}
};


function sendFacebookRequest(message, userIds){
	
	FB.ui(
			{
				method: 'apprequests',
				message: message,
				to: userIds, 
			}, 
			
	requestCallback);
	
	function requestCallback(response) {
		
    }
}


function sendMessageToFbFriend(title, description, userIds, link, picture){
	
	FB.ui(
			{
				method: 'send',
				name: title,
				to: userIds, 
				description: description,
				link: link,
				picture: picture,
			}, 
			
	requestCallback);
	
	function requestCallback(response) {
    }
}

hover_effect_enabled = true;

function addLikeCommentToNewsfeed(uniqueId, commentText, imageSource, like){
	if(commentText != '' && !$j.isEmptyObject($j('#comment_region_'+uniqueId).get(0))){
		var newElement = null;
		if(!like){
			newElement = $j('#user_region_'+uniqueId).find('.event_region').last().clone();
		}else{
			newElement = $j('#user_region_'+uniqueId).find('.event_region').last().prev().clone();
		}
		
		newElement.css('display', 'block');
		newElement.find('.comment_text').html(commentText);
		
		var separator = '<img class=\"img_seperator\" alt=\"seperator\" src='+imageSource+'>';

		$j('#comment_region_'+uniqueId).after(separator);
		newElement.insertAfter($j('#comment_region_'+uniqueId));
		$j('#MyFrentsNewsFeed').masonry();
		//layoutManager.order({itemClass:'.newsfeed_event', parentId: '#MyFrentsNewsFeed', resizableClass: '.center_class', center: true});
		hover_effect_enabled = false;
		$j('#comment_region_'+uniqueId).hide();
	}
}

function addCommentToNewsfeed(uniqueId, commentText, imageSource){
	
	if(commentText != '' && !$j.isEmptyObject($j('#comment_region_'+uniqueId).get(0))){
		var newElement = $j('#user_region_'+uniqueId).find('.event_region').last().prev().prev().clone();
		newElement.css('display', 'block');
		newElement.find('.comment_text').html(commentText);
		
		var separator = '<img class=\"img_seperator\" alt=\"seperator\" src='+imageSource+'>';

		$j('#comment_region_'+uniqueId).after(separator);
		newElement.insertAfter($j('#comment_region_'+uniqueId));
		$j('#MyFrentsNewsFeed').masonry();
		hover_effect_enabled = false;
		$j('#comment_region_'+uniqueId).hide();
		
	}
}

function checkFacebookFriends(data){
    var myObj = eval("("+ data +")");
    if(myObj.id > 1){
        var userIds = new Array(myObj.id);
        sendMessageToFbFriend("Invite to Board", "Invite Facebook friend to Board", userIds, "http://www.pinmag.com" , null);
    }
}



