$(document).ready(function(){
	if ($('.comment_msg').size() > 0) {
		$('.comment_msg').fadeOut().fadeIn().fadeOut().fadeIn();
	};

	shareLink($('.sharelink'));		

	// On the homepage set the fading tweets going.
	revolveTweets();
	
	$('.youtube_link').click(function(){
		vidString = $(this).attr('rel');
		loadMedia(vidString, true, true);
		return false;
	});
	
	// $('.member_video_link').click(function(){
	// 	vidString = $(this).attr('rel');		
	// 	loadMedia(vidString, false, true);
	// 	return false;
	// });
	
	// if ($.cookie('splash') != 2) {
		// if ($('#div_home_release').size() > 0) {
		// 			$.colorbox({href:'splash.php', height: '490', width: '610'});
		// 		
		// 			// if ($.cookie('splash') == 1) {
		// 			// 	$.cookie('splash', 2);
		// 			// } else {
		// 			// 	$.cookie('splash', 1);
		// 			// }
		// 		};
	// };
	
	
	// $('.store_link').click(function(){
	// 	return false;
	// });

	// Add photo form show/hide the gig listing.
	$('.select_category').change( function() {
		if ($(this).val() == 1) {
			$('#select_gig_list').show();
			$('#gig_label').show();			
		} else {
			$('#select_gig_list').hide();
			$('#gig_label').hide();
		}
	});
	
	
	// Add photo form show/hide the gig listing + release list.
	$('.select_blog_category').change( function() {
		if ($(this).val() == 1) {
			$('#select_gig_list').show();http://zaphod.uk.vvhp.net/v-v/100326090632
			$('#gig_label').show();			
			$('#select_release_list').hide();
			$('#release_label').hide();
		} else if ($(this).val() == 2){
			$('#select_release_list').show();
			$('#release_label').show();
			$('#select_gig_list').hide();
			$('#gig_label').hide();
		} else {
			$('#select_gig_list').hide();
			$('#gig_label').hide();
			$('#select_release_list').hide();
			$('#release_label').hide();
		}
	});	
	
	simple_tooltip("a.member_video_link","tooltip");
	
	
	// ==========================
	// = Facebook connect stuff =
	// ==========================
	
	function fbConnect() {
		FB.init("a1904499827763159ebf62cd1e39ef2e", "xd_receiver.htm",{"reloadIfSessionStateChanged":true, "permsToRequestOnConnect" : "email, publish_stream"});
		// FB.init("c9af78cdeb2441d6d45f901cf483a603", "xd_receiver.htm",{"reloadIfSessionStateChanged":true, "permsToRequestOnConnect" : "email, publish_stream"});
	}
	
	// Login facebook button on login page.
	if ($('body#login').size() != 0) {
		fbConnect();
	};	

	if ($('body#register').size() != 0) {
		fbConnect();
	};
	
	// if ($('body#test_publish').size() != 0) {
	// 	FB.streamPublish();
	// 	// alert('yes');
	// };
	
});

function shareLink(button){
	button.mouseover(function(e){
		var link = 'http://' + location.host + '' + $(this).attr('rel');
		var sharehtml = '<span id="share_links"><a href="http://www.facebook.com/share.php?u=' + link + '" rel="external">Facebook</a> / <a href="http://twitter.com/home?status=Currently reading ' + link + '" rel="external">Twitter</a> / <a href="http://digg.com/submit?phase=2&url=' + link + '" rel="external">Digg</a> / <a href="http://del.icio.us/post?url=' + link + '" rel="external">Delicious</a><br /></span>';
		
		if ($('#share_links').size() < 1) {
			//No share links are already on screen so lets show em.
			$('#mainwrap').append(sharehtml);
			$('#share_links').css('top' , e.pageY - 30);
			$('#share_links').css('left' , e.pageX - 85);
		} else {
			// Share link on screen lets fade them out and then show the others.
			$('#share_links').remove();
			$('#mainwrap').append(sharehtml);
			$('#share_links').css('top' , e.pageY - 30);
			$('#share_links').css('left' , e.pageX - 85);
		}
		
	}).mouseout(function(){
		$('#share_links').animate({opacity: 1.0}, 1500).fadeOut('slow', function() {
			$('#share_links').remove();
		});
    });
}


// Function to revolve throught a list of tweets.
function revolveTweets(){
	// First fade out the active_tweet div.
	$('.active_tweet').show();
	$('.active_tweet').animate({opacity: 1.0}, 5000).fadeOut('slow', function() {
		$(this).removeClass('active_tweet');		
		// Now check if there is a next div. If there isn't add the active_tweet class to the first div in the list.
		if ($(this).next('div').size() > 0) {
			$(this).next('div').addClass('active_tweet');
		} else {
			$('.div_tweet:first').addClass('active_tweet');
		}		
		revolveTweets();
	});
};

// Youtube chromeless stuff
function getFlashMovie(movieName) {
	movieName = swfobject.getObjectById(movieName);
	return movieName;
}

function loadMedia(path, youtube, autoStart){
	getFlashMovie("vid_player").loadMedia(path, youtube, autoStart);
}

// Audio player functions
function stopForPopup(){
	var array = getFlashMovie("player").stopForPopup();
	return array;
}

function startMusic(){
	getFlashMovie("player").startMusic();
}

function stopMusic(){
	getFlashMovie("player").stopMusic();
}

function simple_tooltip(target_items, name){
 $(target_items).each(function(i){
		$("body").append("<div class='"+name+" tipper' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
		var my_tooltip = $("#"+name+i);
		if($(this).attr("title") != "" && $(this).attr("title") != "undefined" ){
		$(this).removeAttr("title").mouseover(function(){
						my_tooltip.css({opacity:1.0, display:"none"}).fadeIn(100);
			}).mousemove(function(kmouse){
					var border_top = $(window).scrollTop();
					var border_right = $(window).width();
					var left_pos;
					var top_pos;
					var topoffset = 20;
					var leftoffset = -30;				
					if(border_right - (leftoffset *2) >= my_tooltip.width() + kmouse.pageX){
						left_pos = kmouse.pageX+leftoffset;
						} else{
						left_pos = border_right-my_tooltip.width()-leftoffset;
						}

					if(border_top + (topoffset *2)>= kmouse.pageY - my_tooltip.height()){
						top_pos = border_top +topoffset;
						} else{
						top_pos = kmouse.pageY-my_tooltip.height()-topoffset;
						}	

					my_tooltip.css({left:left_pos, top:top_pos});
			}).mouseout(function(){
					my_tooltip.css({left:"-9999px"});
			});
		}
	});
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
