var _gaq = _gaq || [];

jQuery.fn.setBackgroundPosition = function(val) {
	var p = $(this).css('background-position');
	var temp_pos_val_array=val.split(' ');
	var x_val=temp_pos_val_array[0];
	var y_val=temp_pos_val_array[1];
	if(typeof(p) === 'undefined'){
		$(this).css('background-position-x',x_val);
		$(this).css('background-position-y',y_val);
		return true;
	}else{
		$(this).css('background-position',val);
		return true;
	}
	return false;
};

function BtnOver(el) {
	var p = el.css('background-position');
	if (!el.hasClass('selected')) {
		if (typeof(p) != 'undefined') {
			var posx = parseInt(p);
			el.css('background-position', posx+"px -46px");
		}
		else {
			el.css('background-position-y', '-46px');
		}
	}
}
function BtnOut(el) {
	var posy = "0px";
	if (el.hasClass('selected'))
		posy = "-92px";
	var p = el.css('background-position');
	if (typeof(p) != 'undefined') {
		var posx = parseInt(p);
		el.css('background-position', posx+"px "+posy);
	}
	else {
		el.css('background-position-y', posy);
	}
}
function SetButtonRollovers() {
	$('.btn').mouseover(
		function() {
			BtnOver($(this));
		});
	$('.btn').focus(
		function() {
			BtnOver($(this));
		});
	$('.btn').blur(
		function() {
			BtnOut($(this));
		});
	$('.btn').mouseout(
		function() {
			BtnOut($(this));
		});
	$('.selected').mouseout();
}

function HighlightLoginFields() {
	$('#login-email').animate({backgroundColor:'#53baff'}, 600, 'swing', function() {
		$('#login-email').animate({backgroundColor:'white'}, 300);
		$('#login-password').animate({backgroundColor:'#53baff'}, 600, 'swing', function() {
			$('#login-password').animate({backgroundColor:'white'}, 300);
		});
	});
}

function HighlightBookmarkButton() {
	if (!$.browser.msie) {
		if ($('#bookmarks-count')) {
			$('.btn-bookmarks').stop();
			$('.btn-bookmarks').animate({'opacity':0.2}, 250, "swing");
			$('.btn-bookmarks').animate({'opacity':1}, 250, "swing");
			$('.btn-bookmarks').animate({'opacity':0.2}, 250, "swing");
			$('.btn-bookmarks').animate({'opacity':1}, 250, "swing");
		};
	}
	else {
	}
}

function showLoginOnly(speed) {
	$('#loginform').show();
	$('#loginform').animate({width:"380px"}, speed, "swing", function() { 
		$('#login-email').focus();
		HighlightLoginFields();
	});
	$('#forgotpw').show();
}
var lock_login = false;
function showLogin(speed) {
	if (lock_login)
		return false;
	if(speed===undefined)
		speed = "slow";
	if (!$('#loginform:hidden').length)
		return Login();
	lock_login = true;
	$('#loggedout').hide();
	$('#loginform').show();
	$('#loginform').animate({width:"380px"}, speed, "swing", function() {
		$('#login-email').focus();
		lock_login = false;
	});
	$('#forgotpw').show();
	return false;
}	

function Account() {
	document.location.href="account.php";
	return false;
}

function Login() {
	var email = $('#login-email').val();
	var remember = $('#remember').attr('checked')?1:0;
	var password = sha1Hash($('#login-password').val());
	$('#header').load('login.php', {'email':email, 'password':password, 'remember':remember}, function () {
		if (_gaq) _gaq.push(['_trackPageview', '/login.php']);
		if ($('#login-info').length) { // if logged in
			href = window.location.href;
			if ($('.ab18').length)
				MovieDetail(lastMovieDetail);
			if (href.search(/index.php/)==-1 && href.charAt(href.length-1)!="/") { // if not on main page ...
				if (href.search(/register.php/)!=-1) // register -> main page
					href = "index.php";
				if (href.search(/forgotpw.php/)!=-1) // forgot password -> account
					href = "account.php";
				window.location.href = href;
			}
		}
	});
	return false;
}	
function Logout() {
	$('#header').load('login.php', {'logout':1}, function () {
		if (_gaq) _gaq.push(['_trackPageview', '/login.php?logout=1']);
		href = window.location.href;
		if (href.search(/deposit.php/)!=-1
			|| href.search(/account.php/)!=-1
			|| href.search(/editdata.php/)!=-1
			|| href.search(/creditcard.php/)!=-1
			|| href.search(/changepw.php/)!=-1
				) { 
			window.location.href = "index.php?teasers=1";
		}
		else if (href.search(/index.php/)==-1 && href.charAt(href.length-1)!="/") {
			window.location.href = href;
		}
	});
	return false;
}

function IndicateLoadMovieDetail() {
	$('#moviedetail').css({'opacity':'0.2'});
	$('#moviedetail-loader').css({'display':'block'});
}
function IndicateLoadList() {
	if (!$.browser.msie) {
		$('#list-articles').css({'opacity':'0.2'});
	}
	$('#list-loader').css({'display':'block'});
}
function Lists(type, page) {
	IndicateLoadList();
	$('#lists').load('list.php', {'type':type, 'page':page});
	if (_gaq) _gaq.push(['_trackPageview', '/list.php?type='+type+"&page="+page]);
	return false;
}
function ListsEvent(eventId, page) {
	IndicateLoadList();
	$('#lists').load('list.php', {'type':'event', 'event':eventId, 'page':page});
	if (_gaq) _gaq.push(['_trackPageview', '/list.php?type=event&event='+eventId+"&page="+page]);
	return false;
}
function ListsTag(tagId, page) {
	IndicateLoadList();
	$('#lists').load('list.php', {'type':'tag', 'tag':tagId, 'page':page});
	if (_gaq) _gaq.push(['_trackPageview', '/list.php?type=tag&tag='+tagId+"&page="+page]);
	return false;
}
function ListsAtoZ(letter, page) {
	IndicateLoadList();
	$('#lists').load('list.php', {'type':'atoz', 'letter':letter, 'page':page});
	if (_gaq) _gaq.push(['_trackPageview', '/list.php?type=atoz&letter='+letter+"&page="+page]);
	return false;
}
function ListsGenre(genre, page) {
	IndicateLoadList();
	$('#lists').load('list.php', {'type':'genres', 'genre':genre, 'page':page});
	if (_gaq) _gaq.push(['_trackPageview', '/list.php?type=genres&genre='+genre+"&page="+page]);
	return false;
}
function ListsSearch(query, page) {
	IndicateLoadList();
	$('#lists').load('list.php', {'type':'search', 'q':query, 'page':page});
	if (_gaq) _gaq.push(['_trackPageview', '/list.php?type=search&q='+query+"&page="+page]);
	return false;
}

	function ShowHideNeedsLogin(show) {
	$('.needslogin').css('display', show?'block':'none');
}
function SetBookmarkButtons(bookmarks, downloads) {
	if (!bookmarks)
		bookmarks = new Array();
	if (!downloads)
		downloads = new Array();
	//console.log("SetBookmarkButtons", bookmarks);
	if ($('#bookmarks-count')) {
		if ($('.btn-bookmarks.selected').length) 
			$('#bookmarks-count').setBackgroundPosition((-31*bookmarks.length)+"px -92px");
		else
			$('#bookmarks-count').setBackgroundPosition((-31*bookmarks.length)+"px 0px");
	}
	$('#rollover').hide();
	$('.bm[articleid]').html('<span>Auf die Playlist</span>').removeClass('btn-rem-bookmark');
	$('.bm[articleid]').addClass('btn-add-bookmark');
	$('.bm[articleid]').unbind();
	$('.bm[articleid]').click(function() { AddBookmark($(this).attr('articleid')); });
	for (i in bookmarks) {
		$('.bm[articleid='+bookmarks[i]+']').html('<span>Von Playlist entfernen</span>').removeClass('btn-add-bookmark');
		$('.bm[articleid='+bookmarks[i]+']').addClass('btn-rem-bookmark');
		$('.bm[articleid='+bookmarks[i]+']').unbind();
		$('.bm[articleid='+bookmarks[i]+']').click(function() { RemoveBookmark($(this).attr('articleid'))});
	}
	$('.btn-add-bookmark').setBackgroundPosition("-678px 0px");
	$('.btn-rem-bookmark').setBackgroundPosition("-1004px 0px");
	$('.btn-add-bookmark.btn-small').setBackgroundPosition("-23px 0px");
	$('.btn-add-bookmark.btn-small').mouseover(
		function(e) { $('#rollover').show().html('in Playlist speichern').css({'left':(e.pageX+16)+"px", 'top':(e.pageY-8)+"px"}); }
	).mouseout( function(e) { $('#rollover').hide(); });
	$('.btn-rem-bookmark.btn-small').setBackgroundPosition("0px 0px");
	$('.btn-rem-bookmark.btn-small').mouseover(
		function(e) { $('#rollover').show().html('von Playlist löschen').css({'left':(e.pageX+16)+"px", 'top':(e.pageY-8)+"px"}); }
	).mouseout( function(e) { $('#rollover').hide(); });
	for (i in downloads) {
		$('.bm[articleid='+downloads[i]+']').hide();
	}
}

function AddBookmark(articleid) {
//console.log('AddBookmark', articleid);
	$('#exec').load('bookmark.php', {'action':'add', 'articleid':articleid});
	if (_gaq) _gaq.push(['_trackPageview', '/bookmark.php?action=add&articleid='+articleid]);
}

function RemoveBookmark(articleid) {
//console.log('RemoveBookmark', articleid);
	$('#exec').load('bookmark.php', {'action':'remove', 'articleid':articleid});
	if (_gaq) _gaq.push(['_trackPageview', '/bookmark.php?action=remove&articleid='+articleid]);
}

var lastMovieDetail = 0;
function MovieDetail(articleid) {
	if (!articleid)
		return;
	lastMovieDetail = articleid;
	IndicateLoadMovieDetail();
	$('#content').load('moviedetail.php', {'articleid':articleid});
	if (_gaq) _gaq.push(['_trackPageview', '/moviedetail.php?articleid='+articleid]);
	return false;
}

function displayError(title, msg, needslogin) {
	$('#error').html('<h1>'.concat(title, '</h1><p>', msg, '</p>'));
	$('#error').show();
	if (needslogin) {
		$('#error-background').css({"margin-top":"80px", "opacity": "0.7"}).show();
	}
	else {
		$('#error-background').css({"opacity": "0.7"}).show();
	}
	$(document).bind('click', function(e){ 
		$('#error').hide();
		$('#error-background').hide();
		$(document).unbind('click');
	});
}

function displayMessage(html) {
	$('#message').html(html);
	$('#message').show();
	$('#error-background').css({"opacity": "0.7"}).show();
	$(document).bind('click', function(e){ 
		$('#message').hide();
		$('#error-background').hide();
		$(document).unbind('click');
	});
}

function UpdatesIFR() {
  //console.log("UpdatesIFR");
	var sifrhelv = { src: 'sifr/flash/sifr.swf' };
	sIFR.activate( sifrhelv );
	sIFR.replace(sifrhelv, {selector: 'h1', wmode: 'transparent', css: ['.sIFR-root { color:#000000; font-size:20px; }'] });
	sIFR.replace(sifrhelv, {selector: 'h2', wmode: 'transparent', css: ['.sIFR-root { color:#000000; font-size:20px; }'] });
}
$(document).ready(function() {
	SetButtonRollovers();
	$("a.btn").focus(
		function() {
			this.blur();
		}
	);
});

