jQuery.jQueryRandom = 0;
jQuery.extend(jQuery.expr[":"],
{
    random: function(a, i, m, r) {
        if (i == 0) {
            jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
        };
        return i == jQuery.jQueryRandom;
    }
});

$(function() {
	if (location.pathname.substring(1))
		$('#menu a[href*="' + location.pathname.substring(1,location.pathname.indexOf("/",1)) + '/"]').css({backgroundPosition: '0 100%'});	
		// ... '/"]').css NOT '"]').css
	if (location.pathname.substring(1))
		$('#menu-en a[href*="' + location.pathname.substring(2,location.pathname.indexOf("/",5)) + '/"]').css({backgroundPosition: '0 100%'});	
});

$(function() {
	$(document).ready(function() {
		$('#comment > ul').tabs().tabs('select', 2);
		$('#user > ul').tabs().tabs('select', 1);
	});
});

// Overlay
$(document).ready(function() {

	// Target blank replacement & link tracker
	$('a[rel="external"]').click(function() {
		var domain = window.location.protocol + '://' + window.location.host;
		var url = '/extern/' + $(this).attr('href').replace(domain, '').replace('https://', '').replace('http://', '');
		_gaq.push(['_trackPageview', url]);
		window.open($(this).attr('href'));
		return false;
	});

	// Liveset tracker
	/*
	moved to functions.js
	*/

	// Set the overlay box state
	var isOpen = new Boolean();
	isOpen = false;

	// Redefine opacity for non CSS3 browsers (e.g IE6)
	$(".bgCover").css({opacity:0.5});

	// Set speed for the fade in / fade out effects (accepted values are those of the JQuery functions)
	var fadeSpeed;
	fadeSpeed  = "fast";

	function showOverlayBox(calendar) {
	
		//If box is not set to open then don't do anything
		if( isOpen == false ) return;
		
		// Show overlaybox
		$(".overlayBox").css({display:"block",position:"absolute"});
		$(".closeOverlay").css({display:"block",position:"absolute"});

		if (calendar === undefined) {
			$(".closeLink").css({display:"block",position:"absolute"});
		} else {
			$(".closeOverlayCalendar").css({display:"block",position:"absolute"});
		}

		var menuYloc = null;		
		
		// Function to move the box when opened and scrolled
		scrollOverlay = function() {
			// Get the offset from top of viewport to center the box in the viewport
			var center = ($(window).height() - $(".overlayBox").height()) / 2;
			// Set the offset from top of window to center the box in the viewport
			var offset = $(document).scrollTop()+center+"px";
			// Move the box
			$(".overlayBox").animate({top:offset},{duration:500,queue:false});
			$(".closeLink").animate({top:offset},{duration:500,queue:false});
			$(".closeOverlay").animate({top:offset},{duration:500,queue:false});
			$(".closeOverlayCalendar").animate({top:offset},{duration:500,queue:false});
		}
		// Initiate movement of the box into the viewport when opened
		scrollOverlay();
		
		// Initiate movement of the box when scrolling
		$(window).scroll(function(){
			scrollOverlay();													
		});

		// Deactivate the browser scrollbars (the background is only covering the visible part of the window and if you are able to scroll you'll gain access to the uncovered part)
		// IE needs this!
		if ($.browser.msie){
			$("body").css({overflow:"hidden"});
			$("html").css({overflow:"hidden"});
		}
		
		// Set the window background dimension and position for the overlay.		
		$(".bgCover").css({width: $(document).width(),height:$(document).height(), cursor: "pointer"});
		$(".bgCover").click(doOverlayClose);
	}

	function doOverlayOpen() {
		// Set status to open
		isOpen = true;

		// Show the overlay box
		if ($(this).hasClass('launchLinkCalendar')) {
			showOverlayBox(1);
		} else {
			showOverlayBox();
		}

		// Fade in the background (doing that here to avoid it on the resize event
		$(".bgCover").fadeIn(fadeSpeed);	

		// Don't follow the link : so return false.
		return false;
	}

	function doOverlayClose() {
		setIframe('../includes/loading.php');

		// Set status to closed
		isOpen = false;
		
		// Close the overlay box
		$(".overlayBox").css({display:"none"});
		$(".closeLink").css({display:"none"});
		$(".closeOverlay").css({display:"none"});
		$(".closeOverlayCalendar").css({display:"none"});
		
		// Activate the browser scrollbars
		if ($.browser.msie){
			$("body").css({overflow:"auto"});
			$("html").css({overflow:"auto"});
		}
		
		// Fade out the background
		$(".bgCover").fadeOut(fadeSpeed);
	}

	// if window is resized then reposition the overlay box
	$(window).bind("resize",showOverlayBox);
	// activate when the link with class launchLink is clicked
	$("a.launchLink").click( doOverlayOpen );
	$("a.launchLinkCalendar").click( doOverlayOpen );
	$("a.passwordMessageLink").click( doOverlayOpen );
	$("a.passwordLink").click( doOverlayOpen );
	// close it when closeLink is clicked
	$("img.closeLink").click( doOverlayClose );
	$("img.closeOverlay").click( doOverlayClose );
	$("img.closeOverlayCalendar").click( doOverlayClose );
	
	$('.facebook_share').click(function() {
		var url = encodeURIComponent($(this).attr('href'));
		var title = encodeURIComponent($(this).attr('title'));		
		window.open('http://www.facebook.com/sharer.php?u=' + url + '&t=' + title, 'facebook', 'width=626, height=436');										
		return false;
	});
	
	// cufon	
	Cufon.replace('h1', {
		hover: true
	});
	Cufon.replace('h2', {
		hover: true
	});
	Cufon.replace('h3', {
		hover: true
	});
	Cufon.replace('h4', {
		hover: true
	});
	Cufon.replace('h5', {
		hover: true
	});
	
});

// Tweets
$(document).ready(function() {
	$('.ui-tab-twitter').click(function(){
		$(this).toggleClass('change');
		$('.twitter-stream.inactive').removeClass('active').toggleClass('active');
		$('.twitter-stream.active').toggleClass('inactive');
		return false;
	});
});
