/* -------------------------------------------------------------- 
  
   Pioneer VSX-AV Amplifier Product Site
	 custom.js
   
-------------------------------------------------------------- */


$(document).ready(function () {
														
		//var ie6 = ($.browser.msie && $.browser.version < 7 ) ? true : false;
		//var ie7 = ($.browser.msie && $.browser.version == 7 ) ? true : false;

		// Clear input field on focus 
	  $('.focusField').clearFieldOnFocus();

		// Choose your colour Overlay
		$('#overlay_colourpicker').colourPickerOverlay();
 
	  // Animate Content Background
		$('#content').animatedBackground({bg:['/images/ipod_speaker_system/img/bg_forest.jpg','/images/ipod_speaker_system/img/bg_rock.jpg','/images/ipod_speaker_system/img/bg_space.jpg','/images/ipod_speaker_system/img/bg_beach.jpg']});

		// 
	  $('#signupButton').click( function(e) { submitSignup(); e.preventDefault(); });
});



/* 
    VALIDATE EMAIL
*/

function isEmail(v) {
  return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(v);
}


/* 
    SUBMIT SIGNUP FORM
*/
  
function submitSignup() {
			 
	var $mailFld = $('#MAIL');
	var mail = $('#MAIL').val();
	var lang = $('#LANGUAGE').val();
	
	if (isEmail($mailFld.val())) {
	  $('#errorMsg').hide();
    $($mailFld).removeClass('invalid');   
		
		var url = window.location.href;
		if ( url.indexOf('pioneer.be/nl/') != -1 ) {
			lang = 'benl'
		};
		
		$('#frameElastic').attr('src','http://elasticu.thesedays.be/optiext/optiextension.dll?ID=ZFXTXt6ZxNZZZC&MAIL='+mail+'&LANGUAGE='+lang);
    $('#signupForm').hide();
		$('#thanks').fadeIn();
		
    setTimeout("$('#thanks').hide();$('#signupForm').fadeIn();",10000);					 
	} else {
    $('#errorMsg').fadeIn();
    $($mailFld).addClass('invalid');
	}		

};
 
 
/* 
    ANIMATED BACKGROUND
*/

(function($){
	jQuery.fn.animatedBackground = function(options) {

		return this.each(function() { 
		
			 var bg = options.bg;
			 var nr = 0;
			 var current = 1;
			 
			 $(this).prepend('<div class="bg_1"></div><div class="bg_2"></div>');			 
			 $bg1 = $('.bg_1',$(this));
			 $bg2 = $('.bg_2',$(this));
			 
			 $bg1.css('background-image','url('+bg[nr]+')'); 
			 $bg2.hide(); 
			 
			 jQuery.fn.swap = function() { 
				 nr = nr+1;
				 if (nr >3) nr=0;
				 if (current==1) {
				 	$bg1.fadeOut(2000, function() { $bg2.css('background-image','url('+bg[nr]+')').fadeIn(2000);  })
					current = 2;
				 } else {
				 	$bg2.fadeOut(2000, function() { $bg1.css('background-image','url('+bg[nr]+')').fadeIn(2000);  })
					current = 1;
				 } 
			 }
			 
			 setInterval('jQuery.fn.swap()',10000);
			 
		});
	};
})(jQuery);



/* 
    OVERLAY PICK YOUR COLOUR
*/

(function($){
	jQuery.fn.colourPickerOverlay = function(options) {
		return this.each(function() { 
			 
			 var current;
			 var visible = false;
			 var h = 418; 
			 
			 $overlay = $(this);
		   $items = $('.items div',$overlay);
			 $items.fadeOut();
			 
			 if ( $items.length > 1 ) { 
				 $overlay.append('<div class="close"></div><div class="next"><a href="#"></a></div><div class="prev"><a href="#"></a></div>');
			   $overlay.css({'height':0});
				 $('.close',$(this)).click( function(e) { slideOut(); });
				 $('.next',$(this)).click( function(e) { var i=current+1; if(i>$items.length-1) i=0; jQuery.fn.showItem(i); });
				 $('.prev',$(this)).click( function(e) { var i=current-1; if(i<0) i=$items.length-1; jQuery.fn.showItem(i); });
			 }
			 			
			 jQuery.fn.showItem = function (id) {
				 
					if (!visible) { $overlay.css({'height':0}).animate({'height':h},500, function() {} ); visible = true; };
					
					if ( id != current ) {
						var prevId = current;
						var currId = id;
						if (prevId!=undefined) $($items[prevId]).css('z-index',10);
						$($items[currId]).css('z-index',100).fadeIn('800',function() { if(prevId!=undefined) $($items[prevId]).hide();  });
						current = id;
					} 
		
			 };	 
			 
			 function slideOut() { $overlay.css({'height':h}).animate({'height':0},500); visible = false; $($items[current]).fadeOut(); current = undefined; }
			 
		});
	};
})(jQuery);


/* 
    CLEAR FIELD ON FOCUS 
		For input text where the field label is displayed as initial value of the field 
		when the field is cleared, the initial value - stored in rel attribute - is displayed 
*/
 
(function($){
	jQuery.fn.clearFieldOnFocus = function() {
		return this.each(function() {
      var initVal = ''; if($(this).attr('value') != undefined) initVal = $(this).attr('value'); $(this).attr('rel',initVal); 
			$(this).focus(function() { if($(this).attr('value') == $(this).attr('rel')) $(this).attr('value', ''); });
			$(this).blur(function() { if($(this).attr('value') == '') $(this).attr('value', $(this).attr('rel')); });
		});
	};
})(jQuery);
 
