﻿var Germain = Germain || {};

Germain.HotelBoutique = {};

Germain.HotelBoutique.Magazines = function(opts){
	
	this.opts = $.extend({}, opts, Germain.HotelBoutique.Magazines.DefaultOptions);
	this.page = 0;
	
	this.nodes = {};
	this.nodes.magazines = $(this.opts.magazinesPrefixSel + ' .magazines a');
	this.nodes.nextMagazine = $(this.opts.magazinesPrefixSel + ' a.next');
	this.nodes.prevMagazine = $(this.opts.magazinesPrefixSel + ' a.prev');
	this.nodes.preview = $(this.opts.previewPrefixSel);
	
	this.pageCount = Math.ceil(this.nodes.magazines.length / this.opts.itemPerPage);
	
	this.init();
	this.bindEvents();
};

Germain.HotelBoutique.Magazines.DefaultOptions = {
	magazinesPrefixSel: '.magazines-container',
	previewPrefixSel: '.preview',
	itemPerPage: 15
};

Germain.HotelBoutique.Magazines.prototype.init = function(){
	if(this.nodes.magazines.length > 15)
		this.nodes.nextMagazine.show();
};

Germain.HotelBoutique.Magazines.prototype.bindEvents = function(){
	var self = this;
	
	this.nodes.magazines.bind('click', function(){
		self.show($(this));
	});
	
	this.nodes.nextMagazine.bind('click', function(){
		self.changePage(self.page + 1);
	});
	
	this.nodes.prevMagazine.bind('click', function(){
		self.changePage(self.page - 1);
	});
	
};

Germain.HotelBoutique.Magazines.prototype.changePage = function(page){
	if(page < 0)
		page = 0;
	if(page > this.pageCount - 1)
		page = this.pageCount - 1;
	
	for(var i = this.page * this.opts.itemPerPage; i < this.opts.itemPerPage; i++)
		this.nodes.magazines.eq(i).parent().hide();
	
	for(var i = page * this.opts.itemPerPage; i < page * this.opts.itemPerPage + this.opts.itemPerPage; i++)
		this.nodes.magazines.eq(i).parent().show();		
		
	if(page == 0)
		this.nodes.prevMagazine.hide();
	else
		this.nodes.prevMagazine.show();
		
	if(page == this.pageCount - 1)
		this.nodes.nextMagazine.hide();
	else
		this.nodes.nextMagazine.show();
		
	this.show(page * this.opts.itemPerPage);
		
	this.page = page;
};

Germain.HotelBoutique.Magazines.prototype.show = function(sender){
	sender = typeof sender == 'object' ? sender : this.nodes.magazines.eq(sender);
	
	this.nodes.magazines.parent().removeClass('active');
	sender.parent().addClass('active');
	
	this.nodes.preview.find('img').attr({ 'src': sender.find('img').attr('src').replace('2_', '1_') });
	this.nodes.preview.find('.overlay .title').text(sender.data('title'));
	this.nodes.preview.find('.overlay .bt').attr({ 'href': sender.data('url') });
};

Germain.SocialMediaLounge = function(opts){
	
	this.opts = $.extend({}, Germain.SocialMediaLounge.Options, opts);
	this.dom = {};
	this.dom.widgetContainers = $('.widget-container');
	this.dom.flickr = $('#flickr');
	this.dom.flickrInfos = this.dom.flickr.find('.infos');
	this.dom.wordpress = $('#wordpress');
	this.dom.twitter = $('#twitter');
	this.dom.tweet = this.dom.twitter.find('.template').detach();
	this.dom.tripadvisor = $('#tripadvisor');
	this._init();
};

Germain.SocialMediaLounge.Options = {
	post: {},
	photoset: {},
	photos: [],
	tweets: [],
	flickrMaximumImages:12
};

Germain.SocialMediaLounge.prototype._init = function(){
	var self = this;
	
	setTimeout(function(){
		var baseHeight = $('.widget-container').height();
		var tripadvisorHeight = $('#tripadvisor').height();
		if(baseHeight < tripadvisorHeight)
			$('.widget-container:not(.last)').animate({'height': tripadvisorHeight + 'px'}, 'fast');		
	}, 800);

	// render flickr feed
	self._renderFlickr(this.opts.photoset, this.opts.photos);

	// render latest wordpress posts
	self._renderWordPress(this.opts.post);

	// render latests tweets
	this._renderTwitter(this.opts.tweets);
};

Germain.SocialMediaLounge.prototype._renderWordPress = function(post) {
	var self = this;
	
	this.dom.flickr.find('.loader').fadeOut(1000, function(){
		self.dom.wordpress.removeClass('loading');
	});

	this.dom.wordpress.find('.wordpress-title').text(post.title);
	this.dom.wordpress.find('.wordpress-content').prepend(post.Summary);
	this.dom.wordpress.find('.wordpress-link').attr('href', post.link);
};

Germain.SocialMediaLounge.prototype._renderTwitter = function(data) {
	var self = this;
	var tweets = this.dom.twitter.find('.tweets');
	
	tweets.find('.no-result').toggle(data.length == 0);

	for (var i = 0; i < data.length; i++){
		var tweet = this.dom.tweet.clone(false, false),
			reply = tweet.find('.reply'),
			retweet = tweet.find('.retweet'),
			favorite = tweet.find('.favorite');
		tweet.find('.tweet-content').html(Cossette.Social.Twitter.Utility.parseTweetEntities(data[i].Text));
		tweet.find('.when').text(data[i].HumanFriendlyTimeSpan);
		reply.attr('href', String.format(reply.attr('href'), data[i].Id));
		retweet.attr('href', String.format(retweet.attr('href'), data[i].Id));
		favorite.attr('href', String.format(favorite.attr('href'), data[i].Id));
		tweets.append(tweet);
	}

	this.dom.twitter.find('.loader').fadeOut(1000, function(){
		self.dom.twitter.removeClass('loading');
	});
};

Germain.SocialMediaLounge.prototype._renderFlickr = function(photoset, photos) {
	var self = this,
		updated = Date.parseFromDotNet(photoset.Updated),
		html = '';
	
	this.dom.flickrInfos.find('.photocount').text(photoset.TotalCount);
	this.dom.flickrInfos.find('.viewcount').text(photoset.ViewsCount);
	this.dom.flickrInfos.find('.lastupdate').text(updated.getDate() + ' ' + updated.getMonthName() + ' ' + updated.getFullYear());

	for (var i = 0; i < photos.length && i < this.opts.flickrMaximumImages; i++)
		html += '<a href="'+photos[i].Link+'" target="_blank" class="image">\
					<img src="'+photos[i].XXSmallUrl+'" alt="'+photos[i].Title+'" />\
				</a>';
	
	this.dom.flickr.find('.loader').fadeOut(1000, function(){
		self.dom.flickr.removeClass('loading');
	});

	this.dom.flickr.find('.images').empty().append(html);
	
};

Germain.SocialSummary = function(opts){
	
	this.opts = $.extend({}, Germain.SocialSummary.Options ,opts);

	this._mapDOM();
	this._buildUI();
	this._bindEvents();
};

Germain.SocialSummary.Options = {
	tweets: []	
};

Germain.SocialSummary.prototype._buildUI = function(){
	
	if(this.opts.tweets.length > 0){
		var tweet = this.dom.contents.filter('.twitter'),
			reply = tweet.find('.reply'),
			retweet = tweet.find('.retweet'),
			favorite = tweet.find('.favorite');
		tweet.find('.tweet-content').html(Cossette.Social.Twitter.Utility.parseTweetEntities(this.opts.tweets[0].Text));
		tweet.find('.when').text(this.opts.tweets[0].HumanFriendlyTimeSpan);
		reply.attr('href', String.format(reply.attr('href'), this.opts.tweets[0].Id));
		retweet.attr('href', String.format(retweet.attr('href'), this.opts.tweets[0].Id));
		favorite.attr('href', String.format(favorite.attr('href'), this.opts.tweets[0].Id));	
	}

};

Germain.SocialSummary.prototype._mapDOM = function(){
	this.dom = {};
	this.dom.container = $('#social-summary');
	this.dom.links = this.dom.container.find('.links a');
	this.dom.contents = this.dom.container.find('.social-summary-content>div');
};

Germain.SocialSummary.prototype._bindEvents = function(){
	var self = this;
	
	this.dom.links.bind('click', function(){
		var me = $(this),
			index = me.index(),
			count = self.dom.links.length;
		me.addClass('active').siblings().removeClass('active');
		self.dom.contents.eq(index).removeClass('hidden').siblings().addClass('hidden');
	});

};
