// variables
var logStr = 'Code Log: <br /><br />';

function log(msg) {
	logStr = logStr + msg + '<br />'; writeLayer('codeLogDiv', logStr); // output
}

// write text to a layer
function writeLayer(id, txt) {
	if (document.getElementById) {
		x = document.getElementById(id); x.innerHTML = ''; x.innerHTML = txt;
	} else if (document.all) {
		x = document.all[id]; x.innerHTML = txt;
	} else if (document.layers) {
		x = document.layers[id]; 
		txt2 = '<P CLASS="testclass">' + txt + '<\/p>';
		x.document.open(); x.document.write(txt2); x.document.close();
	}
}

$(document).ready(function() {
	
	var xd = 990;
	var yd = 870;
	
	// Query string variables:
	
	var s = getParameterByName('s'); // location
	var r = getParameterByName('r'); // releases id
	if ((s!='') && (r=='')) { 
		if (s == '0') { // Home
			jx = 0; jy = 0;
		} else if (s == '1') { // Player
			jx = xd+10; jy = 0;
		} else if (s == '2') { // About
			jx = xd*2+20; jy = 0;
		} else if (s == '3') { // Topright
			jx = xd*3+30; jy = 0;
		} else if (s == '4') { // Releases
			jx = 0; jy = yd;
		} else if (s == '5') { // Licensing
			jx = xd*2+20; jy = yd;
		} else if (s == '6') { // Rightmid
			jx = xd*3+30; jy = yd;
		} else if (s == '7') { // Bottomleft
			jx = 0; jy = yd*2; 
		} else if (s == '8') { // Labels
			jx = xd+10; jy = yd*2;
		} else if (s == '9') { // Bottommid
			jx = xd*2+20; jy = yd*2;
		} else if (s == '10') { // Contact
			jx = xd*3+30; jy = yd*2;
		} else if (s == '11') { // Register
			var iframe = $('#supplementary');
			iframe[0].contentWindow.location.href = 'search/register.php';
			jx = xd*3+30; jy = yd;			
		} else if (s == '13') { // Reminder
			var iframe = $('#supplementary');
			iframe[0].contentWindow.location.href = 'search/password.htm';
			jx = xd*3+30; jy = yd;
		} else if (s == '14') { // Login
			var username = getParameterByName('username');
			var password = getParameterByName('password');
			var iframe = $('#ss');
			var terms = $('#terms');
		
			if (username == '' || username == 'username') { alert('Please enter a valid username'); return false; }
			if (password == '') { alert('Please enter a valid password'); return false; }
		
			var url = 'http://ws.harvestmedia.net/login.aspx';
			url += '?username=' + username;
			url += '&password=' + password;
			url += '&acctid=' + $('#login_acctid').val();
			url += '&loginurl=http://www.repertoiremusic.com/invalid.html';
			//url += '&loginurl=' + this.location.href.replace(/\//g, '%2F');
			url += '&dt=' + new Date().getTime();
			iframe[0].contentWindow.location.href = url;
			jx = xd+10; jy = 0;
		} else if (s == '15') { // Search
			var searchterm = getParameterByName('search');
			$('#search').val(searchterm);
			jx = xd+10; jy = 0;
			var iframe = $('#ss');
			var url = 'http://live.harvestmedia.net/player.aspx?acctid=8185d768cd8fcaa7';
			url += '&search=';
			url += searchterm;
			iframe[0].contentWindow.location.href = url;
		}
		// Make the move:
		$('#mover').animate({'left':jx * -1, 'top':jy * -1},'slow');
	};
	if (r!='') {
		jx = 0; jy = yd; // Releases location
		// Load releases item:
		$.ajax({
			type:'POST',
			url:'showrelease.php',
			data:{'id':r},
			success:function(data) {
				$('#releasesrightcontent').html(data);
				var script = 'http://s7.addthis.com/js/250/addthis_widget.js?domready=1';
				if ( window.addthis ) {
					window.addthis = null;
				}
				$.getScript( script );
			}
	    });
		// Make the move:
		$('#mover').animate({'left':jx * -1, 'top':jy * -1},'slow');
	}
	
	// Check whether video release substitute picture is clicked on Home page, if so redirect to Releases page
	$('.vidLink').click(function(){
		var id = $(this).attr('id');
		$.ajax({
			type:'POST',
			url:'showrelease.php',
			data:{'id':id},
			success:function(data) {
				$('#releasesrightcontent').html(data);
				var script = 'http://s7.addthis.com/js/250/addthis_widget.js?domready=1';
				if ( window.addthis ) {
					window.addthis = null;
				}
				$.getScript( script );
				
				// set the scrollbar
				reinitialiseReleasesRightScroll();
			}
	    });
		$('.goLoc').removeClass('goLocCurrent');
		$('#navReleases').children().addClass('goLocCurrent');
		// Make the move:
		$('#mover').animate({'left':0, 'top':-870},'slow');
		return false;
	});
	
	function getParameterByName( name )
	{
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
		return "";
	  else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	}

	
	// Harvest links:
	$('.linkLogin').click(function(){
		doLogin();
		return false;
	});
	$('#harvestlogin').submit(function(){
		doLogin();
		$('.goLoc').removeClass('goLocCurrent');
		$('#navPlayer').children().addClass('goLocCurrent')
		return false;
	});
	$('.reminder').click(function(){
		doReminder();
		$('.goLoc').removeClass('goLocCurrent');
		$('#navRightmid').children().addClass('goLocCurrent')
		return false;
	});
	$('.register').click(function(){
		doRegister();
		$('.goLoc').removeClass('goLocCurrent');
		$('#navRightmid').children().addClass('goLocCurrent');
		return false;
	});
	$('.explore').click(function(){
		doExplore();
		$('.goLoc').removeClass('goLocCurrent');						 
		$('#navExplore').children().addClass('goLocCurrent');
		return false;
	});
	$('.linkSearch').click(function(){
		doSearch();
		return false;
    });
	$('#harvestsearch').submit(function(){
		doSearch();
		return false;
    });
	$('.licencecontact').click(function(){
		$('.goLoc').removeClass('goLocCurrent');
		$('#navContact').children().addClass('goLocCurrent');
		$('#mover').animate({'left':-3000, 'top':-1740},'slow');							
		return false;
	});
	$('.stickley').click(function(){
		$('.goLoc').removeClass('goLocCurrent');
		$('#navBottomleft').children().addClass('goLocCurrent');
		$('#mover').animate({'left':0, 'top':-1740},'slow');							
		return false;
	});
						   
	function doLogin()
	{	
		var username = $('#usernameTextBox').val();
		var password = $('#passwordTextBox').val();
		var iframe = $('#ss');
		var terms = $('#terms');
	
		if (username == '' || username == 'username') { alert('Please enter a valid username'); return false; }
		if (password == '') { alert('Please enter a valid password'); return false; }
	
		var url = 'http://ws.harvestmedia.net/login.aspx';
		url += '?username=' + username;
		url += '&password=' + do_md5(password);
		url += '&acctid=' + $('#login_acctid').val();
		url += '&loginurl=http://www.repertoiremusic.com/invalid.html';
		//url += '&loginurl=' + this.location.href.replace(/\//g, '%2F');
		url += '&dt=' + new Date().getTime();
		iframe[0].contentWindow.location.href = url;
		//terms.style.visibility = 'hidden';
		$('#mover').animate({'left':-1000, 'top':0},'slow');
		return false;
	}
	
	function doSearch()
	{
		var iframe = $('#ss');
		var searchterm = $('#search').val();
		var url = 'http://live.harvestmedia.net/player.aspx?acctid=8185d768cd8fcaa7';
		url += '&search=';
		url += searchterm;
		
		iframe[0].contentWindow.location.href = url;
		$('#mover').animate({'left':-1000, 'top':0},'slow');
		return false;
	}
	
	function doReminder()
	{
		var iframe = $('#supplementary');
		iframe[0].contentWindow.location.href = 'search/password.htm';
		$('#mover').animate({'left':-3000, 'top':-870},'slow');
		return false;
	}
	
	function doRegister()
	{
		var iframe = $('#supplementary');
		iframe[0].contentWindow.location.href = 'search/register.php';
		/*$('#mover').animate({'left':-1000, 'top':0},'slow');*/
		$('#mover').animate({'left':-3000, 'top':-870},'slow');
		return false;
	}
	
	function doExplore()
	{
		$('#mover').animate({'left':-1000, 'top':0},'slow');
		return false;	
	}

	$('a.goLoc').click(function(){
		var destination = $(this).parent().attr('id');
		var go = 0; // to avoid this being activated when the three harvest buttons are clicked.
		// highlight link:
		$('.goLoc').removeClass('goLocCurrent');
		$(this).addClass('goLocCurrent');
		
		// set new coordinates:
		if (destination == 'navHome') { jx = 0; jy = 0; go=1; }
		else if (destination == 'navPlayer') { jx = xd+10; jy = 0; go=1; }
		else if (destination == 'navAbout') { jx = xd*2+20; jy = 0; go=1; }
		else if (destination == 'navTopright') { jx = xd*3+30; jy = 0; go=1; }
		else if (destination == 'navReleases') { jx = 0; jy = yd; go=1; }
		else if (destination == 'navLicensing') { jx = xd*2+20; jy = yd; go=1; }
		else if (destination == 'navRightmid') { jx = xd*3+30; jy = yd; go=1; }
		else if (destination == 'navBottomleft') { jx = 0; jy = yd*2; go=1; }
		else if (destination == 'navLabels') { jx = xd+10; jy = yd*2; go=1; }
		else if (destination == 'navBottommid') { jx = xd*2+20; jy = yd*2; go=1; }
		else if (destination == 'navContact') { jx = xd*3+30; jy = yd*2; go=1; }
		// Make the move:
		if (go == 1) {
		$('#mover').animate({'left':jx * -1, 'top':jy * -1},'slow');
		go = 0;
		}
		return false;
	});
	
	//////////////////////////////////////////////////////////////////////////////////////
	// Home page slide:
	
	$('.embedplayer').click(function(){
		//alert("player clicked!");									 
	});

	var amountToScroll = $('#slide').width();
	var numberOfPics = $('.singleslide').size();
	var autoSlideHome;

	$('#slide_inner').css({'display':'block'});								
	if (numberOfPics > 1) {
		$('#slide_inner').animate({'left':-799},0);
		$('#slide_inner').css({'width':amountToScroll * numberOfPics});
		
		autoSlideHome = setInterval(function (){
			moveHomeRight();				
		},4000);
		
		function moveHomeLeft(){
			var currentPos = Math.abs(parseInt($('#slide_inner').css('left')));
			if (!$('#slide_inner').is(':animated')){
				$('#slide_inner').animate({'left':(currentPos-amountToScroll)*-1},'slow');	
			}
			if (currentPos == (amountToScroll)) { 
				// We're at the start
				$('#slide_inner').animate({'left':((numberOfPics - 2) * amountToScroll)*-1},0);
			}
		}
		
		function moveHomeRight(){
			var currentPos = Math.abs(parseInt($('#slide_inner').css('left')));
			if (!$('#slide_inner').is(':animated')){
				$('#slide_inner').animate({'left':(currentPos+amountToScroll)*-1},'slow');	
			}
			if (currentPos == ((numberOfPics - 2) * amountToScroll)) { 
				// We're at the end
				$('#slide_inner').animate({'left':amountToScroll*-1},0);
			}
		}
		
		$('#moveLeft').click(function(){
			moveHomeLeft();
			clearInterval(autoSlideHome);
			return false;
		});
		
		$('#moveRight').click(function(){
			moveHomeRight();
			clearInterval(autoSlideHome);
			return false;
		});
		
		$('.singleslide').click(function(){
			//alert('slide clicked!');								 
			clearInterval(autoSlideHome);
		});
	}
									
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Labels:
	
	var panelMarginLabel = $('.panel').css('margin-left');
	var amountToScrollLabel = parseInt($('.panel').width()) + parseInt(panelMarginLabel);
	var numberOfPicsLabel = $('.panel').size();
	var autoSlideLabel;
	
	// initialise:
	$('#labelslide_inner').animate({'left':-amountToScrollLabel*2},0); // set initial position
	$('#labelslide_inner').css({'width':amountToScrollLabel * numberOfPicsLabel}); // set width of inner holder to width of all pics
	var currentPosLabel = Math.abs(parseInt($('#labelslide_inner').css('left'))); // get centre label and set relevant info
	var centre = currentPosLabel / amountToScrollLabel+1;
	var labelID = $('.panel:eq('+centre+')').attr('id');
	$('#labelslide_inner .panel').fadeTo(0,0.5); // Set half opacity to all panels
	$('#'+labelID).fadeTo(0,1); // Set full opacity to centre panel
	$('#'+labelID+'2').fadeTo(0,1); // This sets full opacity to the duplicate panel (for endless looping) 
	$('#infobg').children().fadeOut(0); // Remove all label info
	$('#info_'+labelID).fadeIn(0); // Display centre label info
		
	autoSlideLabel = setInterval(function (){
		moveLabelRight();				
	},2000);
	
	function moveLabelLeft(){
		var currentPosLabel = Math.abs(parseInt($('#labelslide_inner').css('left')));	// get current left pos
		if (!$('#labelslide_inner').is(':animated')){	// don't do anything if button is clicked when already moving
			$('#labelslide_inner').animate({'left':(currentPosLabel-amountToScrollLabel)*-1},'slow');	
		}
		if (currentPosLabel == (amountToScrollLabel)) {  // We're at the start, so reset frieze
			$('#labelslide_inner').animate({'left':((numberOfPicsLabel-4) * amountToScrollLabel)*-1},0);
		}
		// Check for centre pic:
		var centre = currentPosLabel / amountToScrollLabel;
		var labelID = $('.panel:eq('+centre+')').attr('id');
		
		// Fade other pics:
		$('#labelslide_inner .panel').fadeTo(200,0.5).delay(300);
		$('#'+labelID).fadeTo(200,1);
		$('#'+labelID+'2').fadeTo(200,1);
		//console.log('currentPosLabel: '+currentPosLabel+'; amountToScrollLabel: '+amountToScrollLabel+'; centre: '+centre);
		
		// Show relevant info:
		$('#infobg').children().fadeOut(0);
		$('#info_'+labelID).fadeIn(0);
	}
	
	function moveLabelRight(){
		var currentPosLabel = Math.abs(parseInt($('#labelslide_inner').css('left')));
		if (!$('#labelslide_inner').is(':animated')){
			$('#labelslide_inner').animate({'left':(currentPosLabel+amountToScrollLabel)*-1},'slow');	
		}
		if (currentPosLabel == ((numberOfPicsLabel - 4) * amountToScrollLabel)) {  // We're at the end
			$('#labelslide_inner').animate({'left':amountToScrollLabel*-1},0);
		}
		// Check for centre pic:
		var centre = (currentPosLabel / amountToScrollLabel)+2;
		var labelID = $('.panel:eq('+centre+')').attr('id');
		if (labelID.indexOf('2') > -1) { var l = labelID.length; labelID = labelID.substr(0,l-1); }
		 
		// Fade other pics:
		$('#labelslide_inner .panel').fadeTo(200,0.5).delay(300);
		$('#'+labelID).fadeTo(200,1);
		$('#'+labelID+'2').fadeTo(200,1);
		//console.log('currentPosLabel: '+currentPosLabel+'; amountToScrollLabel: '+amountToScrollLabel+'; centre: '+centre);
		
		// Show relevant info:
		$('#infobg').children().fadeOut(0);
		$('#info_'+labelID).fadeIn(0);
	}
	
	// Button for move left:
	$('#labelmoveLeft').click(function(){
		moveLabelLeft();
		clearInterval(autoSlideLabel);
		return false;
	});
	
	// Button for move right:
	$('#labelmoveRight').click(function(){
		moveLabelRight();
		clearInterval(autoSlideLabel);
		return false;
	});
	
	$('#infobg').click(function(){							 
			clearInterval(autoSlideLabel);
	});
	
	$('#labelslide_inner').click(function(){							 
			clearInterval(autoSlideLabel);
	});
	
	///////////////////////////////////////////////////////////////////////////////////////
	// RELEASES
	///////////////////////////////////////////////////////////////////////////////////////
	
	// Links:
	
	// set up delegate function for after AJAX is called:
	$('#releasesleftcontent').delegate("a.showreleaselink","click",function(){
		var id = $(this).attr('id');
		// set the scrollbar
		reinitialiseReleasesRightScroll();
		$.ajax({
			type:'POST',
			url:'showrelease.php',
			data:{'id':id},
			success:function(data) {
				// set the content
				$('#releasesrightcontent').html(data);
				
				// add the ADD THIS button
				var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
				if ( window.addthis ) {
					window.addthis = null;
				}
				$.getScript( script );
				
				// set the scrollbar
				reinitialiseReleasesRightScroll();
				//console.log('reinitialised');
			}
	    });
		return false;
	});
	 
	//$("a.showreleaselink").bind('click',function(){ reinitialiseReleasesRightScroll(); });
	
	$('#releasesrightcontent').load(function() { reinitialiseReleasesRightScroll(); });
	
	function reinitialiseReleasesRightScroll()
	{
		//console.log('reinitialising....');
		$('#releasesrightcontent').jScrollPane({
			scrollbarWidth:7,
			dragMaxHeight:10000,
			scrollbarOnLeft:false
		});
		//console.log ('done...');
	}
	
	// month link under archive:
	$('.archivelink').click(function(){
		var date = $(this).attr('id').split(' ');
		var mnth = date[0];
		var year = date[1];
		$.ajax({
			type:'POST',
			url:'setarchive.php',
			data:{'month':mnth,'year':year},
			success:function(data) {
				$('#releasesleftcontent').html(data);	
				// set the scrollbar
				//reinitialiseReleasesRightScroll();
			}
	    });
		return false;
    });
	
	// show release
	$('#releasesleftcontent .showreleaselink').click(function(){
		var id = $(this).attr('id');
		$.ajax({
			type:'POST',
			url:'showrelease.php',
			data:{'id':id},
			success:function(data) {
				$('#releasesrightcontent').html(data);
				//
				var script = 'http://s7.addthis.com/js/250/addthis_widget.js?domready=1';
				if ( window.addthis ) {
					window.addthis = null;
				}
				$.getScript( script );
				
				// set the scrollbar
				reinitialiseReleasesRightScroll();
			}
	    });
		// set the scrollbar
		reinitialiseReleasesRightScroll();
		return false;
    });
	
	// Scrollbars:
	
	$('#releasesleftcontent').jScrollPane({
		scrollbarWidth:7,
		dragMaxHeight:10000,
		scrollbarOnLeft:true
		});
	
	$('#releasesdates').jScrollPane({
		scrollbarWidth:7,
		dragMaxHeight:10000,
		scrollbarOnLeft:false							
	});

	$('#releasesrightcontent').jScrollPane({
		scrollbarWidth:7,
		dragMaxHeight:10000,
		scrollbarOnLeft:false
		});
	
	///////////////////////////////////////////////////////////////////////////////////////
	// BLOG
	///////////////////////////////////////////////////////////////////////////////////////
	
	// Links:
	
	// set up delegate function for after AJAX is called:
	$('#blogleftcontent').delegate("a.showbloglink","click",function(){
		var title = $(this).attr('id');
		$.ajax({
			type:'POST',
			url:'showblog.php',
			data:{'title':title},
			success:function(data) {
				// set the content
				$('#blogrightcontent').html(data);
				
				// add the ADD THIS button
				//var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1';
				//if ( window.addthis ) {
				//	window.addthis = null;
				//}
				//$.getScript( script );
				
				// set the scrollbar
				$('#blogrightcontent').jScrollPane({
					scrollbarWidth:7,
					dragMaxHeight:1000,
					scrollbarOnLeft:false
				});
			}
	    });
		return false;
	});
	
	
	// month link under archive:
	$('.blogarchivelink').click(function(){
		var date = $(this).attr('id').split(' ');
		var mnth = date[0];
		var year = date[1];
		$.ajax({
			type:'POST',
			url:'setblogarchive.php',
			data:{'month':mnth,'year':year},
			success:function(data) {
				$('#blogleftcontent').html(data);				
			}
	    });
		return false;
    });
	
	// show blog
	$('#blogleftcontent .showbloglink').click(function(){
		var title = $(this).attr('id');
		$.ajax({
			type:'POST',
			url:'showblog.php',
			data:{'title':title},
			success:function(data) {
				$('#blogrightcontent').html(data);
				//
				//var script = 'http://s7.addthis.com/js/250/addthis_widget.js?domready=1';
				//if ( window.addthis ) {
				//	window.addthis = null;
				//}
				//$.getScript( script );
				
				// set the scrollbar
				$('#blogrightcontent').jScrollPane({
					scrollbarWidth:7,
					dragMaxHeight:1000,
					scrollbarOnLeft:false
				});
			}
	    });
		return false;
    });
	
	// Scrollbars:
	
	$('#blogleftcontent').jScrollPane({
		scrollbarWidth:7,
		dragMaxHeight:100,
		scrollbarOnLeft:true
		});

	$('#blogrightcontent').jScrollPane({
		scrollbarWidth:7,
		dragMaxHeight:1000,
		scrollbarOnLeft:false
		});
	
});


						   
						   
						   
						   
						   
						   
						   
						   
