$(document).ready(function() {
	//George Biti-NOV 2011
	//hrw7th@yahoo.com
	
	//vars
	var g_totalImages = 13; //# of images in the "gallery" folder
	var g_image1 = Math.floor(Math.random()*13+.99);
	var g_image2 = 0;
	var currPage = 1;
	var currPageR = 1;
	var curriTunes =1;
	var opened = false;
	var totalReleases = null;
	var totalPagesR = null;
	var totalMembers = null;
	var totalPages = null;
	var totaliTunes = null;
	var iTunes = false;
	var artistsOn = false;
	
	//Load members/releases on page load
	var qsp = 0;
	var qspR = 0;
	var qspT = 0;
	
	//functions
	function infoClick(releaseVar) {
		artistsOn=false;
		$("#black-cont").slideDown(300);
		$("#white-cont").slideDown(300, function(){
			if(artistsOn==false){
				//makes X button visible
				$("#closeDesc").css("display", "block");
				//loads new description content (and animates only if scrollbar position is different than the default 0
				if($("#release-info-cont").scrollTop() != 0 && $("#closeDesc").css("margin-left") == "902px") {
					$("#release-info-cont").animate({"scrollTop":"0"}, 300, function(){
						$(this).load("includes/release_des.php?qspD="+releaseVar);
					});
				} else if($("#closeDesc").css("margin-left") != "902px") {//code to execute if artist list visible
					//hides artist desc
					$("#closeDesc2").css("display", "none");
					$("#artist-black-cont, #artist-white-cont").stop().css("width","0px");
					
					//expands desc window
					$("#release-info-cont").html("");
					$("#closeDesc").animate({"margin-left":"902px"},400);
					$("#blackFill").animate({"width":"570px"},400);
					$("#whiteFill").animate({"width":"570px"},400, function(){
						$("#release-info-cont").load("includes/release_des.php?qspD="+releaseVar).scrollTop(0);
					});
				} else {
					$("#release-info-cont").load("includes/release_des.php?qspD="+releaseVar);
				}
			} else {
				//else statement
			}
		});
	}
	function assignBehaviour() {
		//assign fn
		$(".infonow:first").click(function(){
			infoClick(releaseI1)
		});
		$(".infonow:last").click(function(){
			infoClick(releaseI2)
		});
	}
	
	//GETs
	$.get("includes/members2.php?qsp="+qsp, function(data){
		$("#members").append(data);
		//gets the hidden div's text content
		totalMembers = $("#totalNo").text();
		totalPages = Math.ceil(totalMembers/15);
		//divides total number by 15(number of avatars per page), then rounds the result upwards to the nearest integer
		$("#totalPages").html(totalPages);
	});
	
	$.get("includes/releases2.php?qspR="+qspR, function(data){
		$("#releases").append(data);
		//add class to second php generated div (adds margin)
		$(".release-container:odd").addClass("prev-container");
		$.getScript("js/swarm-releases.js");
		assignBehaviour();
		//gets the hidden div's text content
		totalReleases = $("#totalNoR").text();
		//divides total number by 2(number of releases per page), then rounds the result upwards to the nearest integer
		totalPagesR = Math.ceil(totalReleases/2);
		//finds out # of pages for the itunes shop div
		totaliTunes = Math.ceil(totalReleases/5);
		$("#totalPagesR").html(totalPagesR);
		$("#release-info-cont").html("");
	});
		
	//set initial gallery display image
	$("#rightwingbkg1").css("background", "url(images/gallery/gallery"+g_image1+".jpg)");
	
	//image setters
	function imageGen1() {
		//set g_image1 to be g_image2 + 1
		if(g_image2 == g_totalImages) {
			g_image1 = 1;
		} else {
			g_image1 = g_image2 + 1;
		}
		$("#rightwingbkg1").css("background", "url(images/gallery/gallery"+g_image1+".jpg)");
	}
	
	function imageGen2() {
		//set g_image2 to be g_image1 + 1
		if(g_image1 == g_totalImages) {
			g_image2 = 1;
		} else {
			g_image2 = g_image1 + 1;
		}
		$("#rightwingbkg2").css("background", "url(images/gallery/gallery"+g_image2+".jpg)");
	}
	imageGen2();
	
	//fn that fades one image out and then (after a delay) back in
	function changeImage() {
		//fade g_image1 out, increments g_image1 (through imageGen1()), then adds a 5-sec delay
		$("#rightwingbkg1").delay(5000).fadeOut(400, imageGen1).delay(5000);
		//fade g_image1 back in, , increments g_image2 (through imageGen1()), then re-executes the encasing changeImage() function
		$("#rightwingbkg1").fadeIn(400, function(){imageGen2(); changeImage();});
	}
	changeImage();
	
	//Opens (almost) all links in blank windows
	$("a:not('#next-members'>a)").attr("target", "_blank");
	
	/*function to animate and colorize underlaying divs*/
	$.fn.colorize = function(multi, bgcolor1, bgcolor2, bgcolor3){
		$(this).hover(
		function(){
			//colors underlaying divs
			if(multi == false){
				//$("div#color-body > div:not(#color-overlay)>div:first-child").css("background-color", bgcolor1);
			} else {
				$("#leg1-3-underlay").css("background-color", bgcolor1);
				$("#leg2-3-underlay").css("background-color", bgcolor1);
				$("#leg1-2-underlay").css("background-color", bgcolor2);
				$("#leg2-2-underlay").css("background-color", bgcolor2);
				$("#leg1-1-underlay").css("background-color", bgcolor3);
				$("#leg2-1-underlay").css("background-color", bgcolor3);
				$("#body1-underlay").css("background-color", bgcolor3);
				$("#body2-underlay").css("background-color", bgcolor1);
			} 
			$("#leg1-3-underlay").animate({"margin-left":"0px"}, 200);
			$("#leg2-3-underlay").animate({"margin-left":"0px"}, 200);
			$("#leg1-2-underlay").animate({"margin-top":"0px"}, 200);
			$("#leg2-2-underlay").animate({"margin-top":"0px"}, 200);
			$("#leg1-1-underlay").animate({"margin-top":"0px"}, 200);
			$("#leg2-1-underlay").animate({"margin-top":"0px"}, 200);
			$("#body1-underlay").animate({"margin-left":"0px"}, 200);
			$("#body2-underlay").animate({"margin-left":"0px"}, 200);
		},
		function(){
			$("#leg1-3-underlay").stop().css("margin-left", "30px");
			$("#leg2-3-underlay").stop().css("margin-left", "-30px");
			$("#leg1-2-underlay").stop().css("margin-top", "56px");
			$("#leg2-2-underlay").stop().css("margin-top", "-56px");
			$("#leg1-1-underlay").stop().css("margin-top", "-56px");
			$("#leg2-1-underlay").stop().css("margin-top", "56px");
			$("#body1-underlay").stop().css("margin-left", "-207px");
			$("#body2-underlay").stop().css("margin-left", "263px");
		});
	}
	
	//Main button hovers
	
	$("#fb-ul a").hover(
		function(){
			$(this).css("background", "url('images/buttons.png') -60px 0");
			$("#antenna1-underlay").animate({"margin-top":"0px"}, 200);
		},
		function(){
			$(this).css("background", "url('images/buttons.png') 0 0");
			$("#antenna1-underlay").stop().css("margin-top", "60px");
		}).colorize(true, "#223f66", "#304d81", "#3b5998");
	$("#twitter-ul a").hover(
		function(){
			$(this).css("background", "url('images/buttons.png') -60px -176px");
			$("#antenna2-underlay").animate({"margin-top":"0px"}, 200);
			/*$("#twitter-bird")
			.stop()
			.css("margin-left", "700px")
			.fadeIn(200)
			.animate({"margin-left":"850px"}, 200, function(){
				$("#twitter-url").stop().animate({"width":"180px"}, 200);
			});*/
		},
		function(){
			$(this).css("background", "url('images/buttons.png') 0 -176px");
			$("#antenna2-underlay").stop().css("margin-top", "60px");
			/*$("#twitter-bird").stop().css({"display":"none"});
			$("#twitter-url").stop().css({"width":"0"});*/
		}).colorize(true, "#00bbe2", "#44c8ea", "#85d4f2");
	$("#soundcloud-ul a").hover(
		function(){
			$(this).css("background", "url('images/buttons.png') 0 -118px");
		},
		function(){
			$(this).css("background", "url('images/buttons.png') 0 -60px");
		}).colorize(true, "#ff6a2c", "#ff7e17", "#ff9400");
	$("#info").hover(
		function(){
			$(this).css("background", "url('images/buttons.png') -193px 0");
		},
		function(){
			$(this).css("background", "url('images/buttons.png') -121px 0");
		});
	$("#info").click(function(){
		$("#slider").animate({"margin-left":"-931px"}, 600);
		$("#creditsbox").animate({"margin-top": "-15px"}, 200);
		if($("#release-info-cont").html() != ""){
			$("#black-cont, #white-cont").fadeOut(100);
			$("#artist-black-cont, #artist-white-cont").fadeOut(100);
			//makes X button invisible
			$("#closeDesc").css("display", "none");
		}
	});
	$("#back").click(function(){
		$("#slider").animate({"margin-left":"-0px"}, 600);
		$("#creditsbox").animate({"margin-top":"0px"}, 600);
		if($("#release-info-cont").html() != ""){
			$("#black-cont, #white-cont").fadeIn(100);
			$("#artist-black-cont, #artist-white-cont").fadeIn(100);
			//makes X button visible
			$("#closeDesc").css("display", "block");
		}
	});
	$("#back").hover(
		function(){
			$("#back").css("background", "url(images/buttons.png) -147px -155px");
		},
		function(){
			$("#back").css("background", "url(images/buttons.png) -121px -155px");
		}
	);
	$("#creditsbox").hover(
		function(){
			$(this).stop().animate({"height":"16px", "width":"170px"}, 200, function(){
				$(this).html("website | <a class ='js' href='http://horhew.deviantart.com' target='_blank'>george bitiusca</a>").css({"font-size":"12px", "text-align":"center", "color":"#5f5f5f"});
			});
		},
		function(){
			$(this).stop().html("").animate({"height":"15px","width":"15px"}, 200);
		}
	);
	$("#sndot-ul a").hover(
		function(){
			$(this).css("background", "url('images/buttons.png') -161px -91px");
		},
		function(){
			$(this).css("background", "url('images/buttons.png') -157px 0");
		});
	$("#yt-img").hover(
		function(){
			$(this).attr("src", "images/yt-logo-hover.png");
			$("#yt-underlay").animate({"margin-top":"0px"}, 200);
		},
		function(){
			$(this).attr("src", "images/yt-logo.png");
			$("#yt-underlay").stop().css("margin-top", "52px");
		}).colorize(true, "#dc2527", "#ca2327", "red");
	$("#gallery a").hover(
		function(){
			$("#rightwingunderlay").fadeTo(200, 0.3);
		},
		function(){
			$("#rightwingunderlay").stop().css("display", "none");
	});
	$("#player a").hover(
		function(){
			$("#leftwingunderlay").fadeTo(200, 0.3);
			$("#waveform-underlay").animate({"margin-left":"0px"}, 200);
		},
		function(){
			$("#leftwingunderlay").stop().css("display", "none");
			$("#waveform-underlay").stop().css("margin-left", "-314px");
	});
	$("#releases>div").hover(
		function(){
			$(this).css("cursor", "pointer");
			$(this).css("color", "#3f3f3f");
			$(this).children(".release-overlay").fadeTo(0, 0.1);
		},
		function(){
			$(this).css("cursor", "normal");
			$(this).css("color", "#5f5f5f");
			$(this).children(".release-overlay").css("display", "none");
	});	
	$("#theforums a").colorize(true, "#9ab324", "#e2b848", "#d84900");
	
	//Shop
	
	$("#browse-all").click(function() {
		$("#shopCont, #shopContB").slideToggle(300);
		if(iTunes == true){
			$("div#shopContiT, div#shopContBiT").slideToggle(300);
			iTunes = false;
		}
		}).toggle(
			function(){
				opened = true;
				$(this).css("background", "url(images/elements.png) -475px -358px");
			},
			function(){
				opened = false;
				$(this).css("background", "url(images/elements.png) -433px -358px");
		});
	$("#browse-all").hover(
		function() {
			if(opened == false) {
				$(this).css("background", "url(images/elements.png) -433px -358px");
			} else {
				$(this).css("background", "url(images/elements.png) -475px -358px");
			}
		},
		function() {
			if(opened == false) {
				$(this).css("background", "url(images/elements.png) -433px -340px");
			} else {
				$(this).css("background", "url(images/elements.png) -475px -340px");	
			}
		}
	);
	$("#itunes").hover(
		function() {
			$(this).css("background", "url(images/buttons.png) 0 -265px");
		},
		function() {
			$(this).css("background", "url(images/buttons.png) 0 -236px");
		}
	);
	$("#itunes").click(function(){
		$("div#shopContiT, div#shopContBiT").slideToggle(300);
		if(iTunes == false){
			iTunes = true;
			if(!$(".iTunesList").length){
				$("#shopLoad").load("includes/itunes.php?qspT="+qspT, function(data){
					//removes last (empty) php generated <li>
					$(".iTunesList>li:last").remove();
				});
			}
		} else {
			iTunes = false;
		}
		
	});
	$("#ss").hover(
		function() {
			$(this).css("background", "url(images/buttons.png) -124px -278px");
		},
		function() {
			$(this).css("background", "url(images/buttons.png) -121px -197px");
		}
	);
	$("#juno").hover(
		function() {
			$(this).css("background", "url(images/buttons.png) -84px -265px");
		},
		function() {
			$(this).css("background", "url(images/buttons.png) -84px -236px");
		}
	);
	$("#beatport").hover(
		function() {
			$(this).css("background", "url(images/buttons.png) -124px -257px");
		},
		function() {
			$(this).css("background", "url(images/buttons.png) -124px -236px");
		}
	);
	$("#tid").hover(
		function() {
			$(this).css("background", "url(images/buttons.png) 0 -295px");
		},
		function() {
			$(this).css("background", "url(images/buttons.png) -121px -214px");
		}
	);
	$("#previT").hover(
		function(){
			$(this).css("background", "url(images/elements.png) -514px -291px");
		},
		function(){
			$(this).css("background", "url(images/elements.png) -496px -291px");
	});
	$("#previT").click(function(){
		if(curriTunes > 1){
			curriTunes--;
			qspT -= 5;
			$("#shopLoad").load("includes/itunes.php?qspT="+qspT);
		}
	});
	$("#nextiT").hover(
		function(){
			$(this).css("background", "url(images/elements.png) -514px -309px");
		},
		function(){
			$(this).css("background", "url(images/elements.png) -496px -309px");
	});
	$("#nextiT").click(function(){
		if(curriTunes<totaliTunes){
			curriTunes++;
			qspT += 5;
			$("#shopLoad").load("includes/itunes.php?qspT="+qspT);
		}
	});
	
	//Members prev/next
	$("#next").hover(
		function() {
			if(currPage < totalPages) {
				$(this).css("background", "url(images/elements.png) -454px -394px");	
			}
		},
		function() {
			if(currPage < totalPages) {
				$(this).css("background", "url(images/elements.png) -454px -376px");
			} else {
				$(this).css("background", "url(images/elements.png) -454px -412px");
			}
		}
	).mousedown( function(){
		if(currPage != totalPages){
			$(this).css("background", "url(images/elements.png) -496px -394px");
		}
	}).mouseup(function(){
		if(currPage != totalPages){
			$(this).css("background", "url(images/elements.png) -454px -394px");
		}
	});
	$("#prev").hover(
		function() {
			if(currPage > 1) {
				$(this).css("background", "url(images/elements.png) -433px -394px");
			}
		},
		function() {
			if(currPage > 1) {
				$(this).css("background", "url(images/elements.png) -433px -376px");
			}
		}
	).mousedown(function(){
		if(currPage != 1) {
			$(this).css("background", "url(images/elements.png) -475px -394px");
		}
	}).mouseup(function(){
		if(currPage != 1) {
			$(this).css("background", "url(images/elements.png) -433px -394px");
		}
	});
	
	$("#next").click(function(){
		if(currPage < totalPages) {
			qsp += 15;
			$("#members").load("includes/members2.php?qsp="+qsp);
			if(currPage == totalPages-1) {
				$(this).css("background", "url(images/elements.png) -454px -412px");
			}
			currPage++;
			$("#currPage").html(currPage);
			$("#prev").css("background", "url(images/elements.png) -433px -376px");
		}
	});
	$("#prev").click(function(){
		if(currPage > 1) {
			qsp -= 15;
			$("#members").load("includes/members2.php?qsp="+qsp);
			if(currPage == 2) {
				$(this).css("background", "url(images/elements.png) -433px -412px");
			}
			currPage--;
			$("#currPage").html(currPage);
			$("#next").css("background", "url(images/elements.png) -454px -376px");
		}
	});
	
	//Releases prev/next
	$("#nextR").hover(
		function() {
			if(currPageR < totalPagesR) {
				$(this).css("background", "url(images/elements.png) -454px -394px");	
			}
		},
		function() {
			if(currPageR < totalPagesR) {
				$(this).css("background", "url(images/elements.png) -454px -376px");
			} else {
				$(this).css("background", "url(images/elements.png) -454px -412px");
			}
		}
	).mousedown( function(){
		if(currPageR != totalPagesR){
			$(this).css("background", "url(images/elements.png) -496px -394px");
		}
	}).mouseup(function(){
		if(currPageR != totalPagesR){
			$(this).css("background", "url(images/elements.png) -454px -394px");
		}
	});
	$("#prevR").hover(
		function() {
			if(currPageR > 1) {
				$(this).css("background", "url(images/elements.png) -433px -394px");
			}
		},
		function() {
			if(currPageR > 1) {
				$(this).css("background", "url(images/elements.png) -433px -376px");
			}
		}
	).mousedown(function(){
		if(currPageR != 1) {
			$(this).css("background", "url(images/elements.png) -475px -394px");
		}
	}).mouseup(function(){
		if(currPageR != 1) {
			$(this).css("background", "url(images/elements.png) -433px -394px");
		}
	});
	$("#nextR").click(function(){
		if(currPageR < totalPagesR) {
			qspR += 2;
			$("#releases").load("includes/releases2.php?qspR="+qspR, function(){
				//add class to second php generated div (adds margin)
				$(".release-container:odd").addClass("prev-container");
				$.getScript("js/swarm-releases.js");
				//assigns "click" functions to info elements
				assignBehaviour();
			});
			if(currPageR == totalPagesR-1) {
				$(this).css("background", "url(images/elements.png) -454px -412px");
			}
			currPageR++;
			$("#currPageR").html(currPageR);
			$("#prevR").css("background", "url(images/elements.png) -433px -376px");
		}
	});
	$("#prevR").click(function(){
		if(currPageR > 1) {
			qspR -= 2;
			$("#releases").load("includes/releases2.php?qspR="+qspR, function(){
				//add class to second php generated div (adds margin)
				$(".release-container:odd").addClass("prev-container");
				$.getScript("js/swarm-releases.js");
				//assigns "click" functions to info elements
				assignBehaviour();
			});
			if(currPageR == 2) {
				$(this).css("background", "url(images/elements.png) -433px -412px");
			}
			currPageR--;
			$("#currPageR").html(currPageR);
			$("#nextR").css("background", "url(images/elements.png) -454px -376px");
		}
	});
	
	//Release description
	$("#closeDesc, #closeDesc2").hover(
		function(){
			$(this).css("background", "url(images/elements.png) -562px 0");
		}, 
		function(){
			$(this).css("background", "url(images/elements.png) -535px 0");
		}
	);
	$("#closeDesc").click(function(){
		if($("#closeDesc2").css("display") == "block"){ //if artists window open (therefore, if closeDesc 2 is visilbe)
			$("#closeDesc2").css("display","none");
			$("#artist-black-cont").animate({"width":"0px"}, 400);
			$("#artist-white-cont").animate({"width":"0px"}, 400, function(){
				$("#artist-info-cont").html("");
				$("#closeDesc").css("display", "none");
				$("#black-cont").slideUp(300);
				$("#white-cont").slideUp(300, function(){
					$("#release-info-cont").html("");
					$("#blackFill, #whiteFill").css("width","570px");
					$("#closeDesc").css("margin-left","902px");
				});
			});
		} else {
			$(this).css("display", "none");
			$("#black-cont").slideUp(300);
			$("#white-cont").slideUp(300, function(){
				$("#release-info-cont").html("");
				$("#blackFill, #whiteFill").css("width","570px");
				$("#closeDesc").css("margin-left","902px");
			});
		}
	});
	
	//Artists
	$("#closeDesc2").click(function(){
		$(this).css("display", "none");
		$(".listItem").removeClass("focus selected").addClass("nofocus");
		$("#artist-black-cont").animate({"width":"0px"}, 400);
		$("#artist-white-cont").animate({"width":"0px"}, 400, function(){
			$("#artist-info-cont").html("");
		});
	});
	$("#artists").hover(
		function(){
			$(this).css("background", "url(images/elements.png) -451px -437px");
			$("#artists-lid").css("display", "block");
			$("#artists-expander").css({"display":"block", "width":"0px"}).animate({"width":"37px"},200);
		},
		function(){
			$("#artists").css("background", "url(images/elements.png) -433px -437px");
			$("#artists-lid").css("display", "none");
			$("#artists-expander").css({"display":"none", "width":"0px"});
	});
	$("#artists").click(function(){
		if(artistsOn==false || $("#closeDesc").css("display")=="none") {//checks if not already opened
			artistsOn = true;
			$("#release-info-cont").html("");
			if($("#black-cont").css("display") != "none"){
				$("#closeDesc").css("display", "block").animate({"margin-left":"263px"},400);
				$("#blackFill").animate({"width":"156px"},400);
				$("#whiteFill").animate({"width":"156px"},400, function(){
					//execute load
					$("#release-info-cont").load("includes/artists.php", function(){
						$.getScript("js/swarm-artists.js");
					});
				});
			} else {
				$("#blackFill, #whiteFill").css("width","156px");
				$("#black-cont").slideDown(300);
				$("#white-cont").slideDown(300, function(){
					//execute load
					$("#release-info-cont").load("includes/artists.php", function(){
						$.getScript("js/swarm-artists.js");
					});
					$("#closeDesc").css({"display":"block", "margin-left":"263px"});
				});
			}
		}
	});
	
	//Application form
	$("#captcha_code_refresh")
	.hover(
		function(){
			$(this).css("background", "url(images/buttons.png) -173px -175px");
		},
		function(){
			$(this).css("background", "url(images/buttons.png) -173px -155px");
	})
	.click(function(){
		$("#captcha_image").attr("src", "http://www.emailmeform.com/builder/captcha/index/"+Math.random());
	});
	
	$("#captcha_code_about")
	.hover(
		function(){
			$(this).css("background", "url(images/buttons.png) -193px -175px");
		},
		function(){
			$(this).css("background", "url(images/buttons.png) -193px -155px");
	})
	.click(function(){
		window.open('http://www.emailmeform.com/captcha-instruction.html','_blank','width=400, height=500, left=' + (screen.width-450) + ', top=100')
	});
});
