$(document).ready(function() {

gPageTitle = $("title").html();
 $("object").live("mouseenter mouseleave mousedown mouseup",function(){
      document.title = gPageTitle;
 }); 

// Harvest login:

$('#harvestlogin').submit(function(){
		doLogin();
		return false;
	});

function doLogin()
	{	
		var username = $('#usernameTextBox').val();
		var password = $('#passwordTextBox').val();
	
		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.zonemusic.co.uk/invalid.php';
		//url += '&loginurl=' + this.location.href.replace(/\//g, '%2F');
		url += '&dt=' + new Date().getTime();
		//iframe[0].contentWindow.location.href = url;
		//alert(url);
		var player = window.open(url, 'zonemusic', 'height=773, width=980, resizable=no, toolbar=no, titlebar=no, scrollbars=yes, location=no, menubar=no');
		//alert(player);
		if (player)
		{
			player.focus();
		} else {
			//alert('f');	
		}

		return false;
	}
	
/* SLIDESHOW */
	
	var numberOfPics = $('.slide').size();
	var picpos = 1;
	
	function slideLeft() {
		var $active = $('#slideshowleft .slide.active');
		
		if ($active.prev().length) {
			$next = $active.prev();
		} else {
			$next = $('#slideshowleft .slide:last');
			picpos = numberOfPics + 1;
		}
		
		$active.addClass('last-active');

		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
        });
		
		picpos--;
		$('.slidearticle').addClass('slideArticleHidden');
		$('#slidearticle'+picpos).removeClass('slideArticleHidden');
	}
	
	function slideRight() {
		var $active = $('#slideshowleft .slide.active');
		
		//var $next =  $active.next().length ? $active.next() : $('#slideshowleft .slide:first');
	
		if ($active.next().length) {
			$next = $active.next();
		} else {
			$next = $('#slideshowleft .slide:first');
			picpos=0;
		}			
	
		$active.addClass('last-active');

		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
        });
			
		picpos++;
		$('.slidearticle').addClass('slideArticleHidden');
		$('#slidearticle'+picpos).removeClass('slideArticleHidden');
	}
	
	autoSlideHome = setInterval(function() {slideRight();}, 3000 );
	
	$('#ssright').click(function(){
			// hide all players:
			$('.slideshowPlayer').hide();
			// show all "play audio" links:
			$('.slideshowPlayerBtn').show();
			
			slideRight();
			clearInterval(autoSlideHome);
			autoSlideHome = setInterval(function() {slideRight();}, 3000 );
			return false;
		});
	
	$('#ssleft').click(function(){
			// hide all players:
			$('.slideshowPlayer').hide();
			// show all "play audio" links:
			$('.slideshowPlayerBtn').show();
			
			slideLeft();
			clearInterval(autoSlideHome);
			autoSlideHome = setInterval(function() {slideRight();}, 3000 );
			return false;
		});
	
	$('.slideshowright').click(function(){
			//alert('slide clicked!');								 
			clearInterval(autoSlideHome);
		});

	$('.slideshowPlayerBtn').click(function(){
			clearInterval(autoSlideHome);
			var id = $(this).attr('id').replace('slideshowPlayerBtn',''); 
			$(this).hide();
			$('#slideshowPlayer'+id).show();
			return false;
		});

// Carousel: 

	var amountToScroll = 221;
	var numberOfCarouselPics = $('.carouselimg').size();
	var carouselSlideHome;

	$('#slide_inner').css({'display':'block'});								
	if (numberOfCarouselPics > 1) {
		
		var carouselpicpos = 2;
		var initialpoint = -221 * 3;
		$('#slide_inner').animate({'left':initialpoint},0);
		$('#slide_inner').css({'width':amountToScroll * numberOfCarouselPics});
		
		carouselSlideHome = 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');	
				carouselpicpos--;
				//$('.slidearticle').hide();
				//$('#slidearticle'+picpos).show();
			}
			if (currentPos == (amountToScroll)) { 
				// We're at the start
				//alert('start');
				$('#slide_inner').animate({'left':((numberOfCarouselPics - 6) * amountToScroll)*-1},0);
				carouselpicpos = numberOfCarouselPics-4;
				//$('.slidearticle').hide();
				//$('#slidearticle'+picpos).show();
			}
		}
		
		function moveHomeRight(){
			var currentPos = Math.abs(parseInt($('#slide_inner').css('left')));
			if (!$('#slide_inner').is(':animated')){
				$('#slide_inner').animate({'left':(currentPos+amountToScroll)*-1},'slow');	
				carouselpicpos++;
				//$('.slidearticle').hide();
				//$('#slidearticle'+picpos).show();
			}
			if (currentPos == ((numberOfCarouselPics - 4) * amountToScroll)) { 
				// We're at the end
				$('#slide_inner').animate({'left':amountToScroll*-3},0);
				carouselpicpos = 2;
				//$('.slidearticle').hide();
				//$('#slidearticle'+picpos).show();
			}
		}
		
		$('#carouselleft').click(function(){
			// Restore all default display settings:
			$('.carouselimg img').fadeTo(0,1.0);
			$('.carouseltxt').css('visibility','hidden');	
			// Hide all minus buttons:
			$('.carouselminusbutton').css('visibility','hidden');
			// Show all plus buttons:
			$('.carouselplusbutton').css('visibility','visible');
			moveHomeLeft();
			clearInterval(carouselSlideHome);
			return false;
		});
		
		$('#carouselright').click(function(){
			$('.carouselimg img').fadeTo(0,1.0);	
			$('.carouseltxt').css('visibility','hidden');	
			// Hide all minus buttons:
			$('.carouselminusbutton').css('visibility','hidden');
			// Show all plus buttons:
			$('.carouselplusbutton').css('visibility','visible');
			moveHomeRight();
			clearInterval(carouselSlideHome);
			return false;
		});
		
		$('a.carouselpluslink').click(function(){
			// Hide all minus buttons:
			$('.carouselminusbutton').css('visibility','hidden');
			// Show all plus buttons:
			$('.carouselplusbutton').css('visibility','visible');
			// Reset all opacities to full brightness:
			$('.carouselimg img').fadeTo(0,1.0);
			// Hide all texts:
			$('.carouseltxt').css('visibility','hidden');
			// Set THIS opacity to 0.3:
			$(this).parent().parent().children('img').fadeTo(0,0.3);
			// Show THIS text:
			$(this).parent().parent().children('.carouseltxt').css('visibility','visible').fadeTo(0,1.0);
			// Hide THIS plus button:
			$(this).parent().parent().children('.carouselplusbutton').css('visibility','hidden');
			// Show THIS minus button:
			$(this).parent().parent().children('.carouselminusbutton').css('visibility','visible');
			$(this).parent().parent().children('.carouselminusbutton img').fadeTo(0,1.0);
			clearInterval(carouselSlideHome);
			return false;
		});
		
		$('a.carouselminuslink').click(function(){
			// Hide all minus buttons:
			$('.carouselminusbutton').css('visibility','hidden');
			// Show all plus buttons:
			$('.carouselplusbutton').css('visibility','visible');
			// Reset all opacities to full brightness:
			$('.carouselimg img').fadeTo(0,1.0);
			// Hide all texts:
			$('.carouseltxt').css('visibility','hidden');
			clearInterval(carouselSlideHome);
			return false;
		});
		
		$('.carouselimg').click(function(){
			// Hide all minus buttons:
			$('.carouselminusbutton').css('visibility','hidden');
			// Show all plus buttons:
			$('.carouselplusbutton').css('visibility','visible');
			// Reset all opacities to full brightness:
			$('.carouselimg img').fadeTo(0,1.0);
			// Hide all texts:
			$('.carouseltxt').css('visibility','hidden');
			// Set THIS opacity to 0.3:
			$('img',this).fadeTo(0,0.3);
			// Show THIS text:
			$('.carouseltxt',this).css('visibility','visible').fadeTo(0,1.0);
			// Hide THIS plus button:
			$('.carouselplusbutton',this).css('visibility','hidden');
			// Show THIS minus button:
			$('.carouselminusbutton',this).css('visibility','visible');
			$('.carouselminusbutton img',this).fadeTo(0,1.0);
			clearInterval(carouselSlideHome);
		});
	}

/*
$("#carouselleft").CloudCarousel({			
			xPos: 128,
			yPos: 32,
			buttonLeft: $("#left-but"),
			buttonRight: $("#right-but"),
			mouseWheel:true,
			bringToFront:false});
	
$.ajax({
			type:'POST',
			url:'getFirstCarouselAlbum.php',
			success:function(data){
				//alert('DATA: '+data);
				$('#carouselright').html(data);
			}
  	    });

var wrapperHeight = $('#wrapper').height();
$('#wrapperleft').height(wrapperHeight);
$('#wrapperright').height(wrapperHeight);

$('.cloudcarousel').hover(function(){
	$(this).css('cursor','pointer');},
	function(){$(this).css('cursor','auto');});

$('.cloudcarousel').click(function(){
	var id = $(this).attr('id');
	$.ajax({
			type:'POST',
			url:'getCarouselAlbum.php',
			data:{'id':id},
			success:function(data){
				//alert('DATA: '+data);
				$('#carouselright').html(data);
			}
  	    });
});
*/


// The 'Zones':

$('.zonetop').click(function(){
	var id = $(this).attr('id');
	var toshow = '#zonealbums'+id;
	$(toshow).show();
});

$('a.zoneexpand').click(function(){
	var id = $(this).attr('id').replace('zoneexpand','');
	var toshow = '#zonealbums'+id;
	$(toshow).show();	
	
	// hide this button
	$(this).hide();
	// show minus button
	$('#zoneminus'+id).show();
	
	return false;
});

$('a.zoneminus').click(function(){
	var id = $(this).attr('id').replace('zoneminus','');
	var toshow = '#zonealbums'+id;
	$(toshow).hide();	
	
	// hide this button
	$(this).hide();
	// show minus button
	$('#zoneexpand'+id).show();
	
	return false;
});

//

$('#newsletterform').submit(function(){
	var email = $('#newsletteremail').val();
	$.ajax({
			type:'POST',
			url:'notifyNewsletterSubmit.php',
			data:{'email':email},
			success:function(data){
				alert('Thank you - you have been added to the mailing list.');
				$('#newsletteremail').val('');
			}
  	    });
	return false;
});

});


