/*
 * file		custom.js
 * Author	Sabrina Joerg / Razoon AG
 * Date		24.08.2010
 * 
 * !File contains JQuery for Optionmodule of BUI, Javascript of Newslettermodule, Jquery of Gallery Modul!
 * 
 * - JQuery plugin for maxlength of inputfields found on "http://www.stjerneman.com/demo/maxlength-with-jquery"
 * 
 * - Plugin alerts if textfields or selectfields are empty.
 *   Customer cannot put product with empty fields into cart (unless he turns off Javascript).
 *  
 * - Functions for Newsletter to verify customer input. 
 * */

jQuery(document).ready(function(){   
	//get language of customer
	var language = $('meta[name=language]').attr("content");
	var statText;
	
	if(language=='de')
	{
		statText= "Zeichen"; // The status text 
	}
	else if(language=='fr')
	{
		statText= "lettre"; // The status text 
	}	
	else if(language =='en'){
		statText= "characters"; // The status text 
	} 
	
	 $('div.textfield input').maxlength({  
		  	events: ['blur'], // Array of events to be triggerd   
		  	maxCharacters: 12, // Characters limit  
		  	status: true, // True to show status indicator bewlow the element   
		  	statusClass: "status", // The class on the status div 
		  	statusText: statText,
		  	notificationClass: "notification",  // Will be added when maxlength is reached 
		  	showAlert: false, // True to show a regular alert message   
		  	alertText: "You have typed too many characters.", // Text in alert message  
		  	slider: false // True Use counter slider   
	  });
		
	 $('div.textarea textarea').maxlength({  
		    events: [], // Array of events to be triggerd   
		    maxCharacters: 160, // Characters limit  
		    status: true, // True to show status indicator bewlow the element   
		    statusClass: "status", // The class on the status div 
		    statusText: statText,
		    notificationClass: "notification",  // Will be added when maxlength is reached 
		    showAlert: false, // True to show a regular alert message   
			alertText: "You have typed too many characters.", // Text in alert message  
			slider: false // True Use counter slider   
	});

	 //Handle things when customer clicks on submit button
	 $('#product').submit(function(){
		 //If value of textfield (input field) is empty
			if($('div.textfield input').val() == "")
			{
				//get language of customer
				if(language=='de')
				{
					alert("Bitte einen Namen eingeben.");
				}
				else if(language=='fr')
				{
					alert(unescape("Entrez un nom s%27il vous pla%EEt.%0A"));
				}	
				else if(language =='en'){
					alert("Please enter a name.");
				}
				return false;
			}
			//if textarea is empty
			else if($('div.textarea textarea').val() == "")
			{
				//get language of customer
				if(language=='de')
				{
					alert("Bitte einen Text eingeben.");
				}
				else if(language=='fr')
				{
					alert(unescape("Entrez un texte s%27il vous pla%EEt.%0A"));
				}	
				else if(language =='en'){
					alert("Please enter a text.");
				}
				return false;
			}
			//if select value is empty
			else if($('tr.contentrow1 td div.selectList select').val()=="" || $('tr.contentrow2 td div.selectList select').val()=="")
			{
				//get language of customer
				if(language=='de')
				{
					//which select field is empty?
					if($('tr.contentrow1 td div.selectList select').val()=="")
					{
						alert(unescape("Bitte w%E4hlen Sie eine Farbe.%0A"));
					}
					else if($('tr.contentrow2 td div.selectList select').val()=="")
					{
						alert(unescape("Bitte w%E4hlen Sie eine Gr%F6sse.%0A"));	
					}
				}
				else if(language=='fr')
				{
					if($('tr.contentrow1 td div.selectList select').val()=="")
					{
						alert(unescape("Faites votre choix pour l%27option \"couleur\" s%27il vous pla%EEt.%0A"));
					}
					else if($('tr.contentrow2 td div.selectList select').val()=="")
					{
						alert(unescape("Faites votre choix pour l%27option \"taille\" s%27il vous pla%EEt.%0A"));
					}
				}	
				else if(language =='en'){
					if($('tr.contentrow1 td div.selectList select').val()=="")
					{
						alert("Please select a color.");
					}
					else if($('tr.contentrow2 td div.selectList select').val()=="")
					{
						alert("Please select a size");
					}
				}
				return false;			 
			}
			else{
				return true;
			}
		 });
	 });



	function CheckMultiple1(frm, name) {
		for (var i=0; i < frm.length; i++)
		{
			fldObj = frm.elements[i];
			fldId = fldObj.id;
			if (fldId) {
				var fieldnamecheck=fldObj.id.indexOf(name);
				if (fieldnamecheck != -1) {
					if (fldObj.checked) {
						return true;
					}
				}
			}
		}
		return false;
	}

	//Javascript function for Newsletter 
function CheckForm1(f) {
	var email_re = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
if (!email_re.test(f.email.value)) {
	alert("Please enter your email address.");
	f.email.focus();
	return false;
}

		lists_chosen = CheckMultiple1(f, "lists");
		if (!lists_chosen) {
			alert("Please choose some contact lists to subscribe to");
			return false;
		}
	
		lists_chosen = CheckMultiple1(f, "lists");
		if (!lists_chosen) {
			alert("Please choose some contact lists to subscribe to");
			return false;
		}
	
	return true;
}

//Gallery Page
jQuery(document).ready(function($) {


	// We only want these styles applied when javascript is enabled
	$('div.navigation').css({'width' : '400px', 'float': 'left','margin-left':'50px'});
	$('div.content').css({'display' : 'block'});

	// Initially set opacity on thumbs and add
	// additional styling for hover effect on thumbs
	var onMouseOutOpacity = 0.67;
	$('#thumbs ul.thumbs li').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity, 
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast', 
		exemptionSelector: '.selected'
	});
	
	// Initialize Advanced Galleriffic Gallery
	var gallery = $('#thumbs').galleriffic({
		delay:                     2500,
		numThumbs:                 6,
		preloadAhead:              10,
		enableTopPager:            false,
		enableBottomPager:         true,
		maxPagesToShow:            100,
		imageContainerSel:         '#slideshow',
		controlsContainerSel:      '#controls',
		captionContainerSel:       '#caption',
		loadingContainerSel:       '#loading',
		renderSSControls:          true,
		renderNavControls:         true,
		playLinkText:              'Play Slideshow',
		pauseLinkText:             'Pause Slideshow',
		prevLinkText:              '&lsaquo; Previous Photo',
		nextLinkText:              'Next Photo &rsaquo;',
		nextPageLinkText:          '>>',
		prevPageLinkText:          '<<',
		enableHistory:             false,
		autoStart:                 false,
		syncTransitions:           true,
		defaultTransitionDuration: 900,
		onSlideChange:             function(prevIndex, nextIndex) {
		// 'this' refers to the gallery, which is an extension of $('#thumbs')
		this.find('ul.thumbs').children()
			.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
			.eq(nextIndex).fadeTo('fast', 1.0);
		},
		onPageTransitionOut:       function(callback) {
			this.fadeTo('fast', 0.0, callback);
		},
		onPageTransitionIn:        function() {
			this.fadeTo('fast', 1.0);
		}
	});
});





