// Maxeiner functions.js
// Requires: jQuery 1.6.2

jQuery(function(){
	
	// Some global variables and object-patchings
	var jQuerymag = jQuery('.magnifier_img'),
		jQuerylightbox_inner = jQuery('#lightbox_content'),
		jQuerylightbox = jQuery('#lightbox_container'),
		jQueryheadline = jQuery('#lightbox_headline'),
		jQuerydimmer = jQuery('#lightbox_dimmer'),
		jQuerycloser = jQuery('#lightbox_closer'),
		jQueryslidectrl = jQuery('.sliderbox_control'),
		jQueryslidectr = jQuery('.slider_control'),
		jQuerylogin = jQuery('#login_box'),
		jQueryloginbtn = jQuery('#login_btn'),
		jQueryprintbtn = jQuery('#print_btn'),
		jQueryform = jQuery('#contact_form'),
		jQueryrecallform = jQuery('#recall_form'),
		jQueryvbtn = jQuery('.video_btn'),
		jQueryvbox = jQuery('#videobox'),
		jQueryvclose = jQuery('#video_closer'),
		checked = false,
		video = false,
		speed = 500,
		jQuerymaske = jQuery('.mask');
		
	// Defining the functions
	// **********************
	
	// Functions on startup
	jQuerymaske.each(function(){
			var i = jQuery(this), 
				img = i.children();
			i.css({'width' : img.length * 409 + 'px', 'marginLeft' : '-409px'});
			// img.last().insertBefore(img.last());
		});
		
	
	
	// Functions lightbox
	function lb_show(src_param, img_title){
			jQuerydimmer.fadeIn(speed);
			jQuerylightbox.fadeIn(speed, function(){
					jQueryheadline.text(img_title);
					jQuerylightbox_inner.css('backgroundImage', 'url(img/' + src_param + '.jpg)');
					checked = true;
				});
		}
		
	function lb_hide(){
			if(checked === true){
				jQuerydimmer.fadeOut(speed);
				jQuerylightbox.fadeOut(speed, function(){
						jQueryheadline.text('');
						jQuerylightbox_inner.css('backgroundImage', 'url()');
						checked = false;
					});
				};
		}
		
	function magnify(){
			var src =  jQuery(this).attr('alt'), title = jQuery(this).attr('title');	
			if(checked === false) lb_show(src, title);
		}
		
	// Functions videolightbox
	function vbox(){
		var me = jQuery(this);
		//console.info('vbox() called');
		me._showbox = function(){
				jQueryvbox.fadeIn(speed, function(){
					jQueryvclose.fadeIn(speed);
					video = true;
				});
			}
		me._hidebox = function(){
				jQueryvclose.fadeOut(speed, function(){
					jQueryvbox.fadeOut(speed);
					video = false;	
				});
			}
		if(!video){me._showbox()} else {me._hidebox()};
	}
	
	// Function sliderboxes	
	function slide(){
			var slideWidth = 409,
				me = jQuery(this),
				clas = me.hasClass('btn_slide_left'),
				mask = me.siblings('.mask'),
				pic = mask.children();
			me.move_left = function(){
					mask.animate({'marginLeft' : '+=' + slideWidth + 'px'}, speed, function(){
							pic.last().insertBefore(pic.first());
							mask.css('marginLeft', '-409px');
						});
				}
			me.move_right = function(){
					mask.animate({'marginLeft' : '-=' + slideWidth + 'px'}, speed, function(){
							pic.first().insertAfter(pic.last());
							mask.css('marginLeft', '-409px');
						});
				}
			if(clas === true && me.parent('.sliderbox').not('.withtext')){ 
				me.move_left(); 
			} else if(clas === false && me.parent('.sliderbox').not('.withtext')) { 
				me.move_right(); 
			};
		}
		
	function slideText(){
			var slideSpace = 409,
				me = jQuery(this);
			me.name = me.hasClass('btn_move_left');
			me.mask = me.siblings('.mask');
			me.textLayer = me.siblings('.slide_text_layer');
			me.pic = me.mask.children();
			
			me._moveLeft = function(){
				me.mask.animate({marginLeft : '+=' + slideSpace + 'px'}, speed, function(){
					me.pic.last().insertBefore(me.pic.first());
					me.mask.css('marginLeft', '-409px');
					me._complete(0);
				});
			}
			me._moveRight = function(){
				me.mask.animate({marginLeft : '-=' + slideSpace + 'px'}, speed, function(){
					me.pic.first().insertAfter(me.pic.last());
					me.mask.css('marginLeft', '-409px');
					me._complete(2);
				});	
			}
			me._complete = function(pic_num){
				me.textLayer.html('<p><strong>Zentrale Heizanlage auf Hackschnitzelbasis</strong></p><p>' + $(me.pic[pic_num]).attr('title') + '</p>');
			}
			
			if(me.name === true){
				me._moveLeft();	
			} else {
				me._moveRight();
			}
		}
		
	// Function toggling the login
	function toggleLog(e){
			e.preventDefault();
			var slideHeight = jQuery('#login_list').outerHeight();
			jQuerylogin.height() === 0 ? jQuerylogin.animate({height: '+=' + slideHeight + 'px'}, speed) : jQuerylogin.animate({height: '-=' + slideHeight + 'px'}, speed);
		}
		
	// Function check the formulars
	function checkForms(){
			var fname = jQuery('#vorname'),
				lname = jQuery('#nachname'),
				addr = jQuery('#strasse'),
				plz = jQuery('#plz'),
				ort = jQuery('#ort'),
				mail = jQuery('#email'),
				args = {'color' : '#cc0000'};
			if(fname.val() == '') {
					fname.effect('highlight', args, 500).focus();
					return false;
				} else
			if(lname.val() == '') {
					lname.effect('highlight', args, 500).focus();
					return false;
				} else
			if(addr.val() == '') {
					addr.effect('highlight', args, 500).focus();
					return false;
				} else
			if(plz.val() == '') {
					plz.effect('highlight', args, 500).focus();
					return false;
				} else
			if(ort.val() == '') {
					ort.effect('highlight', args, 500).focus();
					return false;
				} else
			if(mail.val() == '') {
					mail.effect('highlight', args, 500).focus();
					return false;
				} else {
					return true;
				}
		}
		
	function checkRecallForm(){
			var fname = jQuery('#vorname'),
				lname = jQuery('#nachname'),
				phone = jQuery('#phone'),
				args = {'color' : '#cc0000'};
			if(fname.val() == '') {
					fname.effect('highlight', args, 500).focus();
					return false;
				} else
			if(lname.val() == '') {
					lname.effect('highlight', args, 500).focus();
					return false;
				} else
			if(phone.val() == '') {
					phone.effect('highlight', args, 500).focus();
					return false;
				} else {
					return true;
				}
		}
		
		
	// Printing the page
	function printPage(e){
			var ok;
			e.preventDefault();
			window.focus();
			if(window.print){
					ok = window.confirm('Seite drucken?');
					if(ok){window.print();}
				}
		}
	
	// Binding functions to elements
	// *****************************
		
	// Lightbox
	jQuerymag.bind('mouseup', magnify);
	jQuerycloser.bind('mouseup', lb_hide);
	jQuerydimmer.bind('mouseup', lb_hide);
	// Sliderboxes
	jQueryslidectrl.bind('mouseup', slide);
	jQueryslidectr.bind('mouseup', slideText);
	// Videobox
	jQueryvbtn.bind('mouseup', vbox);
	jQueryvclose.bind('mouseup', vbox);
	// Login
	jQueryloginbtn.bind('mouseup', toggleLog);
	// Print
	jQueryprintbtn.bind('click', printPage);
	// Submitting forms
	jQueryform.bind('submit', checkForms);
	jQueryrecallform.bind('submit', checkRecallForm);
	
	
	//*************************************************
	// TOOLTIPS
	
	function toggleTooltip(){
		var node = jQuery(this);
		node.tooltip = node.siblings('div');
		
		node._showTooltip = function(){
			node.tooltip.css('display', 'block');
			node.addClass('activeTooltip');
		}
		node._hideTooltip = function(){
			node.tooltip.css('display', 'none');
			node.removeClass('activeTooltip');
		}
		
		if(node.hasClass('activeTooltip')){
			node._hideTooltip();
		} else {
			node._showTooltip();
		}
	}
	
	jQuery('.small_video_btn').bind('mouseenter mouseout', toggleTooltip);
	
	//*************************************************
	// Zertifikate
	
	var all_available_certificates = [],
		already_shown_certificates = -1;
	
	cert_points = jQuery('.cert_point');
	cert_holder = jQuery('.cert_holder');
	
	cert_holder.each(function(){
		all_available_certificates.push(jQuery(this).attr('id'));
	});
	
	var close_certificates_lb = function(){
		jQuery('#certificates_lb').fadeOut(speed, function(){
			jQuery('#certificates_lb_content').empty();	
		});
	}
	
	var show_certs = function(){
		var certificates_total_length = all_available_certificates.length,
			actual_certificate;
		
		switch(jQuery(this).attr('id')){
			case 'get_next_cert' :
				if(already_shown_certificates < certificates_total_length - 1){
					actual_certificate = ++already_shown_certificates;
				} else {
					actual_certificate = certificates_total_length - 1;	
				}
				break;
			case 'get_prev_cert' :
				if(already_shown_certificates > 0){
					actual_certificate = --already_shown_certificates;
				} else {
					actual_certificate = 0;	
				}
				break;
			default				 :
				actual_certificate = cert_points.index(jQuery(this));
				already_shown_certificates = cert_points.index(jQuery(this));
				break;		
		}
		
		var injection = '<img src="img/zertifikate/cert' + all_available_certificates[actual_certificate] + '.jpg" alt="">';
		
		jQuery('#certificates_lb_content').html(injection);
		jQuery('#certificates_lb').fadeIn(speed, function(){
			jQuery('#certificates_lb_close_button').unbind('mouseup').bind('mouseup', close_certificates_lb);
		});
		
		check_for_position(actual_certificate);
		
	}
	
	var check_for_position = function(check_value_param){
		check_value_param === 0 ? jQuery('#get_prev_cert').fadeOut(speed * 0.5) : jQuery('#get_prev_cert').fadeIn(speed * 0.5);	
		check_value_param === all_available_certificates.length - 1 ? jQuery('#get_next_cert').fadeOut(speed * 0.5) : jQuery('#get_next_cert').fadeIn(speed * 0.5);	
	}	
	
	jQuery('#certificates_lb_close_button').bind('mouseup', close_certificates_lb);
	cert_points.bind('mouseup', show_certs);
	
});
