/*
 * jqDnR - Minimalistic Drag'n'Resize for jQuery.
 *
 * Copyright (c) 2007 Brice Burgess <bhb@iceburg.net>, http://www.iceburg.net
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * $Version: 2007.08.19 +r2
 */
(function($){
$.fn.jqDrag=function(h){return i(this,h,'d');};
$.fn.jqResize=function(h){return i(this,h,'r');};
$.jqDnR={dnr:{},e:0,
drag:function(v){
 if(M.k == 'd'){E.css({left:M.X+v.pageX-M.pX + 0,top:M.Y+v.pageY-M.pY});}
 else E.css({width:Math.max(v.pageX-M.pX+M.W,0),height:Math.max(v.pageY-M.pY+M.H,0)});
  return false;},
stop:function(){  if(E.css('top').indexOf('-') !=-1) {E.animate({top:0}, 250);}; E.css('opacity',M.o);$().unbind('mousemove',J.drag).unbind('mouseup',J.stop);}
};
var J=$.jqDnR,M=J.dnr,E=J.e,
i=function(e,h,k){return e.each(function(){h=(h)?$(h,e):e;
 h.bind('mousedown',{e:e,k:k},function(v){var d=v.data,p={};E=d.e;
 // attempt utilization of dimensions plugin to fix IE issues
 if(E.css('position') != 'relative'){try{E.position(p);}catch(e){}}
 M={X:p.left||f('left')||0,Y:p.top||f('top')||0,W:f('width')||E[0].scrollWidth||0,H:f('height')||E[0].scrollHeight||0,pX:v.pageX,pY:v.pageY,k:d.k,o:E.css('opacity')};
 E.css({opacity:0.8});$().mousemove($.jqDnR.drag).mouseup($.jqDnR.stop);
 return false;
 });
});},
f=function(k){return parseInt(E.css(k))||false;};
})(jQuery);


$(document).keyup(function(event){
    if (event.keyCode == 27) {
        hideHelp();
    }
});



var drawer_loaded = false;
var dOpen = false;
var signed_up_dawg = false;
var gotoalltime=false;
function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	 
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	
	return IsNumber;
   
}

function closeStarSubscribeBox()
{
	$.post("/drawer/hideStarSubsribe", {}, function(data){
		if(data == 'good')
		{
			$(".subscribeNotice").hide();
			$("#subNip").hide();
		}
	});
}

function reportProblem()
{
	var ProblemBoxHeight = 390;
	var ProblemBoxWidth = 620;
	var winHeight = $(window).height();
	var new_light_height = (winHeight - ProblemBoxHeight) / 2;
	

	resizeBG();

	
	$(".explainInfoLightboxHolder").css('top', new_light_height);
	//$(".explainInfoLightboxHolder").css('left', ProblemBoxWidth);
	
	$(".explainInfoLightboxBG").show();
	$("#lightBoxContent").load("/about/report", function(){
		//$(".explainInfoLightboxHolder").jqDrag('.jqDrag');
	});
}

function showRSS()
{
	var rss_box = 250;
	//var rssWidth = 620;
	var winHeight = $(window).height();
	var new_light_height = (winHeight - rss_box) / 2;
	

	resizeBG();

	
	$(".explainInfoLightboxHolder").css('top', new_light_height);
	//$(".explainInfoLightboxHolder").css('left', rssWidth);
	
	
	$(".explainInfoLightboxBG").show();
	$("#lightBoxContent").load("/about/rss", function(){
		//$(".explainInfoLightboxHolder").jqDrag('.jqDrag');
	});	
}

function logInOrSignUp()
{
	var logInOrSignUpBoxHeight = 400;
	var logInOrSignUpBoxWidth = 620;
	var winHeight = $(window).height();
	var new_light_height = (winHeight - logInOrSignUpBoxHeight) / 2;		
		
	if($(".explainInfoLightboxHolder").is(":visible"))
		$(".explainInfoLightboxHolder").animate({'top': new_light_height});	
	else
	{
		$(".explainInfoLightboxHolder").css('top', new_light_height);
		$(".explainInfoLightboxBG").show();
	}
		
	resizeBG();
	$("#lightBoxContent").load("/loginorsignup/index", function(){});
}

function addBand(name)
{
	
	if(name!='')
		name = name.replace(/ /g, "%20");
	else
		name = "";
	
	
	var addBandBoxHeight = 400;
	//var addBandBoxWidth = 620;
	var winHeight = $(window).height();
	var new_light_height = (winHeight - addBandBoxHeight) / 2;
	
	resizeBG();

	
	$(".explainInfoLightboxHolder").css('top', new_light_height);
	//$(".explainInfoLightboxHolder").css('left', addBandBoxWidth);
	
	$(".explainInfoLightboxBG").show();
	$("#lightBoxContent").load("/edit_profile/add_band/"+name, function(){
		//$(".explainInfoLightboxHolder").jqDrag('.jqDrag');
		
	});	
	
}

function resizeBG(padding)
{
	if(padding == null || padding == 'undefined')
		padding = 50;
	
	var body_height = $("body").height();
	var win_height = $(window).height();
	
	//alert(body_height);
	
	var new_height = win_height;
	if(body_height > win_height)
		new_height = body_height;
	else
		new_height = win_height;
	
	$(".explainInfoLightboxBG").css('height', new_height+padding);
	
	$("body").css("overflow", "hidden");
	
}

function showHelp()
{
	
	var faq_box_height = 524;
	var FAQBOXWIDTH = 620;
	var winHeight = $(window).height();
	var new_light_height = (winHeight - faq_box_height) / 2;		
		
	$(".explainInfoLightboxHolder").css('top', new_light_height);
	//$(".explainInfoLightboxHolder").css('left', FAQBOXWIDTH);
	
	resizeBG();
	$(".explainInfoLightboxBG").show();
	$("#lightBoxContent").load("/about/faq", function(){
		//$(".explainInfoLightboxHolder").jqDrag('.jqDrag');
	});
	
}

function hideHelp(box)
{
	if(box==undefined)
		box='';
	
	
	if(box=='edit')
	{
		if($("#completeProfiles").html()=='0')
		{
			$("#save_changes").val('Save Changes');
			$("#save_changes").attr('disabled', false);
			alert('Please enter at least one profile');
			
			return;
		}
	}
	
	$(".explainInfoLightboxBG").hide();
	$("#lightBoxContent").html('<div class="lightboxInnerHolder" style="height: 50px; line-height:50px;"><div style="padding-left:10px;">Loading...</div></div>');
	$("body").css("overflow", "auto");
		
	switch(window.location.hash)
	{
		case '#add':
		case '#track':
		case '#login':
		case '#signup':
			window.location.hash='';
			break;
		default:
			break;
	}
}

function editURL(id)
{
	resizeBG();
	
	var edit_url_box = 550;
	var winHeight = $(window).height();
	var new_light_height = (winHeight - edit_url_box) / 2;
	
	$(".explainInfoLightboxHolder").css('top', new_light_height);

	
	$(".explainInfoLightboxBG").show();
	$("#lightBoxContent").load("/edit_profile/index/"+id, function(){
		//$(".explainInfoLightboxHolder").jqDrag('.jqDrag');
	});	
}

function editYoutube(id)
{
	resizeBG();
	
	var edit_url_box = 550;
	var winHeight = $(window).height();
	var new_light_height = (winHeight - edit_url_box) / 2;
	
	$(".explainInfoLightboxHolder").css('top', new_light_height);

	
	$(".explainInfoLightboxBG").show();
	$("#lightBoxContent").load("/edit_profile/editYoutube/"+id, function(){
		//$(".explainInfoLightboxHolder").jqDrag('.jqDrag');
	});	
}

function showStarSubscribe()
{
	
	
	var subscribe_star_box = 220;
	var winHeight = $(window).height();
	var new_light_height = (winHeight - subscribe_star_box) / 2;
	
	if(page == 'compare')
		resizeBG(180);
	else if(page == 'artist')
		resizeBG(250);
	else
		resizeBG();

	
	$(".explainInfoLightboxHolder").css('top', new_light_height);
	$(".explainInfoLightboxHolder").css('background', 'white');
	
	$(".explainInfoLightboxBG").show();
	$("#lightBoxContent").load("/drawer/starSubscribe", function(){
		
	});
}

var signup=false;

function inlineSignUp(id)
{
	if(signed_up_dawg)
	{
		$("#pleaseVerify").show();
		$("#signingYouUp").hide();
	}
	else
	{
		var name = $(".g"+id+" a.name").html();
		
		
		
		$("#artistTiles a:not(.s"+id+")").removeClass('starSelected');
		
		if(!$("#artistTiles .s"+id).hasClass('starSelected'))
		{
			
			
			$("#artistTiles .s"+id).addClass('starSelected');
			
			
			var color = $(".g"+id).parent().css('background-color');
		
			$(".signUpGroupName").css("color", color);
			$(".signUpGroupName").html(name);
			//alert($("#signUpInlineName"));
			
			if($("#artistTiles .starSelected").attr('class')!=undefined)
			{
				$("#newSignUpInline").show();
				$("#signUpBoxInline").hide();
			}
		}
		else
		{
			$("#artistTiles .s"+id).removeClass('starSelected');
		}
		
		if($("#artistTiles .starSelected").attr('class')==undefined)
		{
			$("#newSignUpInline").hide();
			$("#signUpBoxInline").show();
		}
		
		if($("#emailAddy").is(":visible"))
			$("#emailAddy").select();
	}
}

function add_artist(id)
{
	$(".globalMessage").slideUp(0);
	$("#group_id_subscribe").val(id);
	if(!signup)
	{
		$.post("/ajax/star_artist", {group_id: id}, function(data) {
			if(data=='good')
			{			
				$("#artistTiles .s"+id).addClass('starSelected');
				$("#artistTiles .s"+id).attr("href", "javascript:remove_artist('"+id+"');");
				
				
				$(".notEnoughYet .s"+id).addClass('starESelected');
				$(".notEnoughYet .s"+id).attr("href", "javascript:remove_artist('"+id+"');");
				
				$(".notEnoughYet .s"+id).html("Unsubscribe from stats");
				
				
				if(!dOpen)
					$("#drawer").load("/drawer");
				else
					$("#drawer").load("/drawer/solo");
				
				
				$("#topBar").prepend('<div class="globalMessage">Subscribed!<\/div>');
				setTimeout(function(){
								$(".globalMessage").slideUp(200);
							},2000);
				
			}
			else if(data=='signup')
			{
				signup = true;
				inlineSignUp(id);
					
			}
			else
				alert('Oops! An error occured. Please refresh and try again!');
	
		});
	}
	
	if(signup)
		inlineSignUp(id);
}

function emailSignUpReports()
{
	//first need to find out if they already have an account.
	
	$("#signUpInlineErrors").html('');
	$("#subscribeInline").attr('disabled', 'true');
	var email = $("#emailAddy").val();
	var pass = $("#usrPass").val();
	var groupid = $("#group_id_subscribe").val();
	
	//lets check the email first before moving forward
	$.post("/ajax/login_or_email", {email: email}, function(data){
		if(data != 'Enter a real email address!')
		{
			if(data == 0)
			{
				//then create account, subscribe them to the artist
				
				$.post("/ajax/ajax_signup", {signupEmail: email, signupPassword: pass, group_id: groupid}, function(data){
					if(data == 'signedUp')
					{
						$("#loggingYouIn").hide();
						$("#signingYouUp").show();
						$("#emailAddyForm").hide();
						signed_up_dawg = true;
					}
					else
					{
						$("#signUpInlineErrors").html(data);
						$("#subscribeInline").removeAttr('disabled');
					}
				});
				
			}
			else
			{
				//log them in and subscribe to the artist
				
				$.post("/ajax/ajax_login", {email: email, password: pass, group_id: groupid}, function(data){
					if(data == 'refresh')
					{
						$("#loggingYouIn").show();
						$("#emailAddyForm").hide();
						location.reload(1000);
					}
					else
					{
						$("#signUpInlineErrors").html(data);
						$("#subscribeInline").removeAttr('disabled');
					}
				});
				
				
			}
		}
		else
		{
			$("#signUpInlineErrors").html(data);
			$("#subscribeInline").removeAttr('disabled');
		}
	});
	
	
}

function remove_artist(id)
{
	
	
	var artist_name =$("#artistTiles .s"+id).next().html();
			
	//var remArtist = confirm("Are you sure you want to remove " + artist_name + "? This will also unsubscribe you from any future emails about " + artist_name + ".");
			
	$.post("/ajax/unstar_artist", {group_id: id}, function(data) {
			if(data=='good')
			{				
				$("#artistTiles .s"+id).removeClass('starSelected');
				$("#artistTiles .s"+id).attr("href", "javascript:add_artist('"+id+"');");
				
				
				$(".notEnoughYet .s"+id).removeClass('starESelected');
				$(".notEnoughYet .s"+id).addClass('starE');
				$(".notEnoughYet .s"+id).attr("href", "javascript:add_artist('"+id+"');");
			
				$(".notEnoughYet .s"+id).html("Subscribe to stats");
				
				
				if(!dOpen)					
					$("#drawer").load("/drawer");
				else
					$("#drawer").load("/drawer/solo");
				
				$("#artistTiles .s"+id).addClass('star');
				$("#topBar").prepend('<div class="globalMessage">Unsubscribed! <a href="javascript:add_artist(\''+id+'\');">Undo</a>.<\/div>');
				setTimeout(function(){
							$(".globalMessage").slideUp(200);
						},10000);

			}
			else
				alert('Oops! An error occured. Please refresh and try again!');
	
		});
	
}

function signUpAjax(append)
{
	if(append==undefined)
		append = "";
	
	var signUpBoxHeight = 500;
	var signUpBoxWidth = 620;
	var winHeight = $(window).height();
	var new_light_height = (winHeight - signUpBoxHeight) / 2;
	
	
	if($(".explainInfoLightboxHolder").is(":visible"))
		$(".explainInfoLightboxHolder").animate({'top': new_light_height});	
	else
	{
		$(".explainInfoLightboxHolder").css('top', new_light_height);
		$(".explainInfoLightboxBG").show();
	}
	
	resizeBG();

	$("#lightBoxContent").load("/signup/ajax/"+append, function(){});
		
}

function loginAjax(append)
{	
	if(append==undefined)
		append = "";
	
	var loginBoxHeight = 400 + 50;
	var loginBoxWidth = 620;
	var winHeight = $(window).height();
	var new_light_height = (winHeight - loginBoxHeight) / 2;		
		
	if($(".explainInfoLightboxHolder").is(":visible"))
		$(".explainInfoLightboxHolder").animate({'top': new_light_height});	
	else
	{
		$(".explainInfoLightboxHolder").css('top', new_light_height);
		$(".explainInfoLightboxBG").show();
	}
		
	resizeBG();
	$("#lightBoxContent").load("/login/ajax/"+append, function(){});
	
}

function checkShowSubscribe()
{
	if(page=='compare' || page == 'artist')
	{
		$.post("/drawer/showStarSubsribe",{}, function(data){
			
			//alert(data);
			if(data=='show')
			{
				showStarSubscribe();
			}
		});
	
	}
}

function loginPop()
{
	
	var loginboxheight = 324;
	var FAQBOXWIDTH = 620;
	var winHeight = $(window).height();
	var new_light_height = (winHeight - loginboxheight) / 2;		
		
	$(".explainInfoLightboxHolder").css('top', new_light_height);
	//$(".explainInfoLightboxHolder").css('left', FAQBOXWIDTH);
	
	resizeBG();
	$(".explainInfoLightboxBG").show();
	$("#lightBoxContent").load("/login/ajax", function(){
		//$(".explainInfoLightboxHolder").jqDrag('.jqDrag');
	});
	
}


$(document).ready(function(){
	
	var window_hash = window.location.hash;

	switch(window_hash)
	{
		case '#add':
			addBand('');
			break;
		case '#track':
			showHelp();
			break;
		case '#login':
			loginAjax();
			break;
		case '#signup':
			signUpAjax();
		default:
			break;
	}





	
	$("#emailAddy").keyup(function(){
		$("#emailAddy").css('color', '#666666');
	});
	
	$("#emailAddy").focus(function(){
	
		$("#emailAddy").css('color', '#666666');
		
		
		
		$("#emailAddy").select();
	});
	
	$("#emailAddy").blur(function(){
		if($(this).val() == '')
		{
			$(this).val('email address');
			$(this).css('color', '#aaaaaa');
		}
	});
	
	$("#usrPass").blur(function(){
		if($("#usrPass").val() == '')
		{
			$("#usrPass").hide();
			$("#usrPassText").show();			
		}
	});
	
	$("#usrPassText").focus(function(){
		
		$(this).hide();
		$("#usrPass").show();
		$("#usrPass").css('color', '#666666');
		$("#usrPass").focus();
		$("#usrPass").select();
	});
	
	
	$(window).bind('resize', function() {
		$(".explainInfoLightboxBG").css('height', $("body").height()+10);
	});
	
	
	$("#toggleDrawer").click(function(){	
		dOpen = !dOpen;
		
		if(!drawer_loaded)
		{
			$("#topBar").prepend('<div class="globalMessage">Loading...<\/div>');

			$("#drawer").load("/drawer", function(){
				
				$("#drawer ul").slideDown(200);	
				
				$(".globalMessage").slideUp(200);
				
			});
			drawer_loaded = true;
		}
		else
		{
			$("#drawer ul").slideToggle(250);
		}

		
		return false;
	});
	
	
	$("#clearSearchDude").click(function(){
		$(".compareInput").val('');
		$(".combands").val('');
		$("#query1").focus();
		//window.location.hash = '';
		return false;
	});
	
	
	
		
});