/////////////////////////////////////////////////////////
// Prey Public JS
// By Tomas Pollak
// (c) 2010-2012 - Fork Ltd.
// MIT Licensed
/////////////////////////////////////////////////////////

String.prototype.capitalize = function() {
	return this.charAt(0).toUpperCase() + this.slice(1);
}

$(document).ready(function(){

	// Prey.revealUpperbar();
	Prey.trackDownloadLinks();

	$('a#badge').click(function(e){

		showOverlay('#announcement');
		return false;

	});

	$('.scroll').click(function(e){
		e.preventDefault();
		$.scrollTo($(this).attr('href'), 300);
	});

	if($('body#home')[0]){ // home page

		Prey.detectOS();
		Slider.initialize();
		$('a.video').click(function(){
			showOverlay('#video-intro');
			return false;
		})

		if(location.href.indexOf('#') != -1){
			gotoTab(location.href.split('#')[1]);
		}

	} else {

		if($('#troubleshooting #content')[0]) Prey.generateSidebar();

		if($('#faq #content')[0]){

			$('.entry h3').each(function(i,el){
				$(this).click(function(){
				// $(this).prepend(i*1+1 + '. ').click(function(){
					$(this).next().slideToggle('fast');
				}).next().hide();

			});

		}

	}

});

var os = 'windows';

var Prey = {

	revealUpperbar: function(){
		$('#upperbar').slideDown('fast');
	},

	detectOS: function(){

		ua = navigator.userAgent;
		if (ua.indexOf("Mac") != -1) os = 'mac';
		else if (ua.indexOf("Ubuntu") != -1) os = "ubuntu";
		else if (ua.indexOf("Linux") != -1) os = "linux";
		else if (ua.indexOf("Android") != -1) os = "android";
		else os = 'windows';
		// else return;

		if (!links[os]) return;

		// $('.download').attr('href', links[os]['link']).not('.white').fadeOut('fast',function(){
		// 	$(this).html(links[os]['text']).fadeIn('slow');
		// });

		var download_text;

		$('.download').attr('href', links[os]['link'])
		.hover(
			function(){
				download_text = download_text || $(this).find('span').html();
				$(this).find('span').fadeOut('fast', function(){
					$(this).html('v' + prey_version + ' for ' + os.capitalize()).fadeIn('slow');
				});
			},
			function(){
				$(this).find('span').fadeOut('fast', function(){
					$(this).html(download_text).fadeIn('slow');
				});
			}
		);

	},

	trackDownloadLinks: function(){

		$('a.download').click(function(){

			var link = $(this).attr('href');
			// if(typepfpageTracker) pageTracker._trackPageview(link);
			_gaq.push(['_trackPageview', link]);
			var download_link = 'http://panel.preyproject.com/signup?download=' + link.replace('/releases', '');
			window.location = download_link;
			
			/*

			var link = this.href;
			// eliminamos el http:// con el hostname en caso de lo que tenga
			if(link.match(/http:/)) link = link.substring(7+this.hostname.length,link.length);
			if(pageTracker) pageTracker._trackPageview(link);
			showOverlay('#download-info');
			
			*/
			
			return false;

		});

	},

	generateSidebar: function(){

			var html = '';
			$('#box').append('<div id="sidebar"></div>');
			$('#content').find('h3').each(function(i,el){

				html += '<div><h3><a href="#' + $(el).attr("id") + '">' + $(el).html() + '</a></h3><ul class="'+$(el).attr("id")+'">';

				$(el).click(function(){

					$(this).next().slideToggle('fast');

				}).next('div').slideUp().children('h4').each(function(a,t){

					html += '<li><a href="#' + $(t).attr("id") + '">' + $(t).html() + '</a></li>'

				});

				html += '</ul></div>';

			});

			$('#sidebar').append(html);

			$('#sidebar li a, .entry a[href^="#"]').click(function(e){
				e.preventDefault();
				var target = $(this).attr('href');
				$(target).parent('div').slideDown().siblings('div').slideUp('fast', function(){
					$.scrollTo(target, 500, {offset: -50});
				});
			});

			$('#sidebar h3 a').click(function(e){
				e.preventDefault();
				var target = $(this).attr('href');
				$(target).next('div').slideDown().siblings('div').slideUp('fast', function(){
					$.scrollTo(target, 500, {offset: -50});
				});
			});

			$('.troubleshooting h5').attr('title', $('.troubleshooting h5').html());

	}

}

function gotoTab(anchor){
	a = $('#menuitem-' + anchor + ' a');
	if (a[0]){
		// showDiv('#' + anchor, a);
		$.scrollTo('#' + anchor, 500, {offset: -78});
		// Slider.moveTo(a, false);
		// $.scrollTo('#' + anchor, 500, {offset: -78, onAfter:function(){ Slider.moveTo(a, false) } });
	}
}

function tabSize(a){
	if($(a).parent().hasClass('selected') || $(a).hasClass('loading')) return;
	target = '#' + $(a).attr('href').split("#")[1];
	showDiv(target, a);
}

function showDiv(target, a){
	// mostrar target
	$(target).fadeIn(400).siblings('div').not('.locked').hide();
	// cambiar clases en link
	$(a).removeClass('loading').parent().addClass('selected').siblings().removeClass('selected'); // el loading es en caso de que tuviera

}

function showOverlay(div){

	if (!$(div)[0]) return;
	var closeable = arguments[0] || true;

	// the opacity thingy doesn't work well on firefox and camino on mac, so we have to use a PNG background
	var ua = navigator.userAgent.toLowerCase();
	if (ua.indexOf('mac') != -1 && (ua.indexOf('firefox')!=-1 || ua.indexOf('camino')!=-1)) {
		$('#overlay').removeClass('opacity').addClass('macffhack');
	}

	$('#overlay').show();
	$(div).show();

	if(closeable){
		// close it on click outside the box content
		$('#overlay').click(function(){ hideOverlay(div); });
		// close it on ESC keypress if the overlay is visible
		$(document).keyup(function(e) {
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
			if (key == 27 && $('#overlay').is(':visible')) {
				hideOverlay(div);
			}
		});
	}
	return false;
};

function hideOverlay(div){
	$('#overlay').hide();
	$(div).hide();
};
