/**
 * jQuery javascript file for all effects on the website hair-nail.de.
 * 
 * @creator				Michael Schulze <m.schulze@elsigno.de>
 * @file					energie.js
 * @license				all rights reserved; no usage without written permission
 * @requirements	jquery-1.5.1.min.js
 * 							  jquery.tools.min.js
 * 							  jquery.hoverIntent.minified.js
 * 
 *  Copyright (C) <2010> <Michael Schulze>
 *  
 */

// no conflict mode so jQuery can be used with other libraries too.
jQuery.noConflict();

var path = 'fileadmin/styles/energie';     // Path to the template directory

// Language vars
if (language = 'de') {
	var llimage = 'Bild';
	var llfrom = 'von';
	var llto = 'bis';
} else if ( language = 'es') {
	var llimage = 'Imagen';
	var llfrom = 'de';
	var llto = 'a';
} else if ( language = 'en') {
	var llimage = 'Image';
	var llfrom = 'from';
	var llto = 'to';
}

jQuery(document).ready(function($){
	accessifyhtml5($);
	
	questions($);

  // supply the language
  $.tools.validator.localize("de", {
  	'*'			: 'Bitte füllen SIe dieses Feld aus',
  	':email'  	: 'Es wird eine E-Mail-Adresse benötigt',
  	':number' 	: 'Arvon on oltava numeerinen',
  	':url' 		: 'Virheellinen URL',
  	'[max]'	 	: 'Arvon on oltava pienempi, kuin $1',
  	'[min]'		: 'Arvon on oltava suurempi, kuin $1',
  	'[required]'	: 'Kentän arvo on annettava'
  });

  $("form").validator({
    errorClass: 'error',
    lang: 'de',
    offset: [0,0],
    position: 'center left'
  });
  
  popups($);
  
  if ($("*").is("#formula")) {
    window.onload = formfocus;
  }

});

function formfocus() {
  document.getElementById('formula').focus();
}


function popups($) {
  $('.popupContainer').show();
  $('.popup-1, .popup-2, .popup-3, .popup-4').each(function(){
    var cssClass = $(this).attr('class');
    if ($('.popupContainer #'+cssClass).length > 0) {

      $(this).addClass('popupStyle');

      var offset = $(this).position();
      $('.popupContainer #'+cssClass).css({
        position: 'absolute',
        top: offset.top + 20,
        left: offset.left,
        'z-index': 250
      });
      
      $(this).hoverIntent({
    		over: function(){
    			$('.popupContainer #'+cssClass).fadeIn(100);
    		},  
    		timeout: 300, // number = milliseconds delay before onMouseOut    
    		out: function(){
    		  $('.popupContainer #'+cssClass).fadeOut(300);
    		}
    	});
    }
  });
  
  return true;
}

function questions($) {
	if ($("*").is("form#questions")) {
		var questions = $('form#questions')
		,		panels = questions.find('.panels')
		,		scrollable = panels.find('.scrollable')
		,   formContainer = panels.find('#panel-5 .form')
		,   wrongContainer = panels.find('#panel-5 .wrong')
		,   wrong;
		
		//questions.find('.steps ul').centerize();
		
		questions.find('.enableJs').hide();
		panels.show();
		
		var scrollWidth = panels.find('.panel').length * panels.find('.panel:first').outerWidth();
		scrollable.css('width',scrollWidth);
		
		// change to last panel if cookie is set
		if ($.cookie('energiequestions')) {
		  panels.scrollTo({top:0, left:scrollWidth},100, {easing:'swing'});
		  questions.find('.steps li').removeClass('current');
			questions.find('.steps li:last').addClass('current');
			wrongContainer.hide();
		}

		questions.find('.question input').change(function(){
			var panelId = $(this).parents('.panel').attr('id');

			questions.find('.steps li.'+panelId).removeClass('current').next('li').addClass('current');

			if (questions.find('.steps li:last').hasClass('current')) {
			  questions.find('.question input:checked').each(function(){
			    if ($(this).val() != 1) { wrong = true; }
			  })
			  if (wrong == true) {
			    wrongContainer.show();
			    formContainer.hide();
			  } else {
			    wrongContainer.hide();
			    formContainer.show();
			    $.cookie('energiequestions', 'answered', { expires: 1 });
			  }
			}
			
			panels.scrollTo({top:0, left:'+=760px'},200, {easing:'swing'});

		})

    questions.find('.wrong a.button').click(function(){
      questions.find('.question input').each(function(){
        $(this).removeAttr("checked");
      });
      questions.find('.steps li').removeClass('current');
      questions.find('.steps li:first').addClass('current');
      panels.scrollTo({top:0, left:0},300, {easing:'swing'});
      wrong = false;
      return false;
    })
	}
}

function questionsOld($) {
	if ($("*").is("form#questions")) {
		var questions = $('form#questions')
		,		panels = questions.find('.panels')
		,		scrollable = panels.find('.scrollable');
		
		//questions.find('.steps ul').centerize();
		
		questions.find('.enableJs').hide();
		panels.show();
		
		var scrollWidth = panels.find('.panel').length * panels.find('.panel:first').outerWidth();
		scrollable.css('width',scrollWidth);
		
		// change to last panel if cookie is set
		if ($.cookie('energiequestions')) {
		  panels.scrollTo({top:0, left:scrollWidth},100, {easing:'swing'});
		  questions.find('.steps li').removeClass('current');
			questions.find('.steps li:last').addClass('current');
		}

		questions.find('.question input').change(function(){
			var panelId = $(this).parents('.panel').attr('id');

			if ($(this).val() == 1) {
				panels.scrollTo({top:0, left:'+=760px'},200, {easing:'swing'});
				questions.find('.steps li.'+panelId).removeClass('current').next('li').addClass('current');
				if ($(panelId).next('.panel').height() > 300) {
					panels.height($(panelId).next('.panel').height())
				}
				
			} else {
				panels.scrollTo({top:0, left:'+=200px'},200, {easing:'swing'})
				.scrollTo({top:0, left:'-=200px'},200, {easing:'swing',
					onAfter:function(){
						//in callback to queue in
						questions.find('div#' + panelId + ' .errorbox').fadeIn(200);
					}
				})
			}
			
			// set cookie
			if (questions.find('.steps li:last').hasClass('current')) {
			  $.cookie('energiequestions', 'answered', { expires: 1 });
			}
		})

	}
}

/**
 * Accessifyhtml5.js
 * Adds ARIA to new elements in browsers which don’t do it by themselves.
 *
 * originally by Eric Eggert
 * https://github.com/yatil/accessifyhtml5.js/blob/master/accessifyhtml5.js
 *
 **/
function accessifyhtml5($){
	var fixes = {
		'header.site'   : { 'role':          'banner'        },
		'footer.site'   : { 'role':          'contentinfo'   },
		'article'       : { 'role':          'article'       },
		'aside'         : { 'role':          'complementary' },
		'nav'           : { 'role':          'navigation'    },
		'output'        : { 'aria-live':     'polite'        },
		'section'       : { 'role':          'region'        },
		'[required]'    : { 'aria-required': 'true'          }
	};

	$.each(fixes, function(index, item) {
		$(index).attr(item);
	});
}

(function( $ ){

  var methods = {
    init : function( options ) {
      var defaults = {
        placeBy         : 'margin' /* margin or position*/
      };
      
      return this.each(function() {
        var $this = $(this)
          , o = $.extend(defaults, options || {});
        
        $this.width($this.width()+2).css({
          display: 'block',
          marginLeft: 'auto',
					marginRight: 'auto',
          'float': 'none'
        });
      })
      
    },
    update: function(content){
      
    }
  };

  $.fn.centerize = function( method ) {
    
    // Method calling logic
    if ( methods[method] ) {
      return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      return methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    }    
  
  };

})( jQuery );
