$(document).ready(function(){
	$("#recommended").tabbed();
	$("#album a img, #checkout a img, #archive a img").tooltip();
	$("#loginform input.text").placeholder();
	/*if($("#archive li").length) {
		$("#archive").jcarousel({
			scroll: 1,
			wrap: "both",
			visible: 4
		});
	}*/ 
	Cufon.replace("h2 span, h3 span, h3, h2, label:not(.ignore), #campaign p, button, .songcount, .jcarousel-item span, .big_message h1, .big_message p, #login_register .tab, #archive-container ul span");
	
	/*if($("#archive li").length<5) {
		$(".jcarousel-next, .jcarousel-prev").hide();
		$(".jcarousel-container").css("margin-left", "0");
	}*/
	
});


$.fn.tabbed = function(opts) {
	$(this).each(function() {
		var tabbed = this;
		$(".wrap>div", this).hide();
		$(".tabs a", this).each(function() {
			$(this).click(function() {
				var active = $(this).attr("href");
				$(".sel", tabbed).removeClass("sel");
				$(this).parent("li").addClass("sel");
				
				$(active).show().siblings("div").hide();
				return false;
			});
		});
		$(".tabs li:first-child a", this).trigger("click");
	});
};

$.fn.tooltip = function(opts) {
	$(this).each(function() {
		var tooltip;
		var elm = this;
		$(elm).parent("a").mouseenter(function(){
			tooltip = $("<div />").addClass("tooltip");
			tooltip.text($(elm).attr("alt")).hide();
			// $(elm).after(tooltip.fadeIn(400));
			
			$("body").append(tooltip.fadeIn(400));
			$(document).mousemove(function(e) {
				tooltip.css({
					position: "absolute",
					left: e.pageX+12,
					top: e.pageY
				});
			});
			
		}).mouseleave(function() {
			tooltip.remove();
		});
	});
};

$.fn.placeholder = function(opts) {
	$(this).each(function() {
		var placeholder = $(this).val();
		$(this).focus(function() {
			if(placeholder == $(this).val())
			$(this).val("");
		}).blur(function() {
			if(placeholder == $(this).val() || $(this).val() =="")
				$(this).val(placeholder);
		});
		
	});
};

var owc_facebook_connect = {
	init: function(){
		FB.init({
			appId		: 198256233523691,
			xfbml		: true
		});
		
		owc_facebook_connect.parse();
		
	},
	parse: function(){
		$('.fb_login:not(.parsed)').addClass('parsed').wrapInner('<fb:login-button perms="email" autologoutlink="false"></fb:login-button>');
		FB.XFBML.parse();
	}
};

var cross;
var owc_register = {
	has_facebook: false,
	init: function() {
		$('.register-facebook-iframe').click(function(e){
			e.preventDefault();
			FB.login( function(response){
				if ( response.session && response.perms ) {
					owc_register.step1( '?facebook=true' );
				}
			}, { 'perms' : 'email' } )
		});
		$('.register-iframe').click(function(e) {
			e.preventDefault();
			owc_register.step1();
		});
	},
	step1: function( query ) {
		//$.ajax({
		//	url			: '/registrera-lightbox',
		//	success	: function(data) {
			
				cross = $('body').crosslog({
					content	: '<iframe id="register-frame" src="/registrera-lightbox' + ( query ? query : '' ) + '" style="height: 470px" scrolling="no" frameborder="0"></iframe>'
				});
		//	}
		//});
	}
};

function owc_register_resize(width, height) {
	cross.crosslog_repos(width, height);
	//console.log('w ' + width + ' h ' + height );
	//$('#crosslog iframe').height(1100);
	//$('#crosslog').height(1100);
}

var owc_login = {
	init: function() {
		$('.login-iframe').click(function(e) {
			e.preventDefault();
			owc_login.step1();
		});
	},
	step1: function() {
		//$.ajax({
		//	url			: 'login_crosslog.html',
		//	success	: function(data) {
				
				
				cross = $('body').crosslog({
					content	: '<iframe id="login-frame" src="/logga-in-lightbox" style="height:230px; width: 390px" scrolling="no" frameborder="0"></iframe>'
					,width: 390
					//,height	: 360
				});
				
				//$('#crosslog iframe').height(290);
				
		//	}
		//});
	}
};

$(function() {
	
	owc_facebook_connect.init();
	owc_register.init();
	owc_login.init();
	
	if ( window.location.search.indexOf('?register-lightbox') > -1 && window.location.search.indexOf('reloaded=true') < 0 ) {
		var c = window.location.search.split('c=');
		var query = '';
		if ( c && c.length > 1 ) {
			query = '?c=' + c[1];
		}
		owc_register.step1(query);
	}
		
});
