$(document).ready(function(){
	$('a.music').click(function(){
		$(this).toggleClass("on");
		if ($(this).is('.on')) {
			$(this).text('Music Off');
			$('#musicframe').attr('src','musica.html');
			createCookie('audio','on','1');
		} else {
			$(this).text('Music On');
			$('#musicframe').attr('src','');
			createCookie('audio','off','1');
		}
	});
	
	var x = readCookie('audio');
	if (x == null) {
		createCookie('audio','on','1');
	} 
	
	if (x == 'on') {
		$('a.music').addClass('on');		
		$('a.music').text('Music Off');
		$('#musicframe').attr('src','musica.html');
	} else if (x == 'off') {
		$('a.music').removeClass('on');		
		$('a.music').text('Music On');
		$('#musicframe').attr('src','');	
	}
	
  /* S L I D E S H O W */
  var currentPosition = 0;
  var slideWidth = $('#slidesContainer').width();
  var slides = $('.slide');
  var numberOfSlides = slides.length;
  $('#slidesContainer').css('overflow', 'hidden'); // Remove scrollbar in JS

  // Wrap all .slides with #slideInner div
  slides.wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
	.css({
      'float' : 'left',
      'width' : slideWidth
    });
  
  $('#slideInner').css('width', slideWidth * numberOfSlides); // Set #slideInner width equal to total width of all slides

  // Insert controls in the DOM
  $('#slideshowProd')
    .prepend('<span class="control" id="leftControl">Clicking moves left</span>')
    .append('<span class="control" id="rightControl">Clicking moves right</span>');

  manageControls(currentPosition); // Hide left arrow control on first load
  
  $('.control').bind('click', function(){ // Create event listeners for .controls clicks
	currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1; // Determine new position    	
    manageControls(currentPosition); // Hide / show controls
    $('#slideInner').animate({ // Move slideInner using margin-left
      'marginLeft' : slideWidth*(-currentPosition)
    });
  });

  function manageControls(position){ // manageControls: Hides and Shows controls depending on currentPosition
	if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() } // Hide left arrow if position is first slide	
    if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() } // Hide right arrow if position is last slide
  }
  
});

function slideSwitch() {
	var $this = $('.pics');
	var $active = $('IMG.active',$this);	
	if ( $active.length == 0 ) $active = $('IMG:last',$this);
	var $next =  $active.next().length ? $active.next()	: $('IMG:first',$this);
	$active.addClass('last-active');
	$next.css({opacity: 0.0}).addClass('active').animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
	});
}
setInterval( "slideSwitch()", 5000 );
		
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
