var echo = function(object, showAlert) {
  if(typeof console == 'object') {
    console.log(object);
  } else {
    if(showAlert)
      alert(object);
  }
}

var lastAjaxUrl = '';

$(function(){
	
	$('.vimeo-lightbox').click(function(event){
		var html = '<div class="vimeoLightBox"><a onclick="Boxy.get(this).hide(); return false;" href="#" class="close">X</a>' +
			'<iframe id="vimeoplayer" src="http://player.vimeo.com/video/___VIMEO_ID___?api=1&amp;player_id=vimeoplayer&amp;title=0&amp;byline=0" width="505" height="278" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>' + 
			'</div>';
		
		html = html.replace('___VIMEO_ID___', $(this).attr('rel'));

		new Boxy(html, {
			modal: true,
			unloadOnHide: true,
			closeable: false
		});
		event.preventDefault();
	});
	
});

function iniPage() {
	var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
	if (window.addthis){
		window.addthis = null;
	}
	$.getScript(script);
	$('.do-ajax-link').click(function(event){
		openPage($(this).attr('href'));
		event.preventDefault();
	});
	$('.do-flash-link').click(function(event){
		getFlash("Nutcase").openMember($(this).attr('rel'));
		event.preventDefault();
	});
}

function closePage() {
	$("#main-wrapper").hide();
	$("#flash").css("height", "100%");
	$f('vimeoplayer').api('pause');
}

function openPage(url) {
	$("#main-wrapper").show();
	$("#flash").css("height", "60px");
	
	if (lastAjaxUrl != url && url != undefined) {
		lastAjaxUrl = url;
		$('.arrow').remove();
		$('html, body').scrollTop(0);
		$('body').css({background: '#000', height: $(window).height() + 'px'})
		$('<div id="overlay" />').css({
			position: 'absolute',
			background: '#000 url(' + basePath + '/css/main/images/preloader.gif) no-repeat center',
			left: 0,
			right: 0,
			top: 0,
			height: '100%',
			overflow: 'hidden',
			zIndex: 140
		}).appendTo('body');
		$.ajax(url, {
			dataType: 'html',
			success: function(data) {
				setTimeout(function(){
					var wrapper = data.match(/<div id="main-wrapper"([\s\S]*?)<!--#main-wrapper-->/gim)[0].replace('<!--#main-wrapper-->', '');
					$('#main-wrapper').replaceWith(wrapper);
					if (iniPage) {
						iniPage();
					}
					$('#overlay').remove();
					$('body').removeAttr('style');
				}, 500);
			}
		});
	}
}

function getFlash(movieName) {
  return navigator.appName.indexOf("Microsoft") != -1 ? window[movieName] : document[movieName]
}

