﻿/*
	func.js
	2011-02-16
------------------------------------------------------ */
$(document).ready(function(){

	// sliding doors
	var slideDelay = setTimeout(function() {
		var slide = $('.sliding-doors');
		var width = slide.outerWidth();
		slide.find('.door-left').animate({ left : -width },{queue:false,duration:1500});
		slide.find('.door-right').animate({ right : -width },{queue:false,duration:1500});
	}, 2000);
	$('.sliding-doors').click(function(){
		slideDelay;
	});

	// auto-adjust height of stains...
	var whatHeight = (typeof document.body.style.maxHeight === "undefined") ? 'height' : 'min-height';
	$(".stains").each(function(){
		var highest = 0;
		var $cols = $(this).find("li");
		$cols.each(function(){
			var $col = $(this);
			var h = $col.outerHeight();
			if (h > highest) {highest=h;}
		});
		$cols.each(function(){
			$(this).css(whatHeight, highest);
		});
	});

	// click in list fade
	$(".fade li").click(function(){
		var theLink = $(this).find("a").attr("href");
		if (theLink) {
			window.location = theLink;
			return true;
		}
		return false;
	});

	// show /hide text
	$('.cut_text .read_more').click(function(){
		$(this).parent().parent().find('p').css('display','block');
		$(this).css('display','none');
		return false;
	});

	var dt =  new Date();
	$("#datesubmitted").val(dt.getTime());

	// toggle contact list
	$(".split").delegate("h3", "click", function(){
		var tmpNode = $(this).parent().parent();
		tmpNode.find('div.address').hide();
		tmpNode.find('h3').removeClass("min");
		$(this).addClass("min");
		$(this).next('div.address').show();
		$.fancybox.resize();
		adjustFancybox();
	});

	// tooltip NOTE
	$('.tooltip.note').before('<img src="css/img/icon-note.gif" class="tooltip" />');
	$('img.tooltip').click(function(){
		return false;
	}).focus(function(){
        var pp = $(this).offset().left;
        if (pp < 600) {
            $(this).next('.tooltip.note').css("margin", "20px 0 0 30px");
        } else {
            $(this).next('.tooltip.note').css("margin", "20px 0 0 -330px");
        }
		$(this).next('.tooltip.note').css($(this).offset()).show();
		return false;
	}).blur(function(){
		$(this).next('.tooltip.note').hide();
		return false;
	}).hover(function(){
        var pp = $(this).offset().left;
        if (pp < 600) {
            $(this).next('.tooltip.note').css("margin", "20px 0 0 30px");
        } else {
            $(this).next('.tooltip.note').css("margin", "20px 0 0 -330px");
        }
		$('p.note:visible').hide();
		$(this).next('.tooltip.note').css($(this).offset()).show();
	},function(){
		$(this).next('.tooltip.note').hide();
	});
 
	// tooltip NEW on products
	$('.tooltip.new').before('<img src="css/img/icon-new.gif" class="tooltip" />');
	$('img.tooltip').click(function(){
		return false;
	}).focus(function(){
        var pp = $(this).offset().left;
        if (pp < 600) {
            $(this).next('.tooltip.new').css("margin", "20px 0 0 30px");
        } else {
            $(this).next('.tooltip.new').css("margin", "20px 0 0 -330px");
        }
		$(this).next('.tooltip.new').css($(this).offset()).show();
		return false;
	}).blur(function(){
		$(this).next('.tooltip.new').hide();
		return false;
	}).hover(function(){
        var pp = $(this).offset().left;
        if (pp < 600) {
            $(this).next('.tooltip.new').css("margin", "20px 0 0 30px");
        } else {
            $(this).next('.tooltip.new').css("margin", "20px 0 0 -330px");
        }
		$('p.new:visible').hide();
		$(this).next('.tooltip.new').css($(this).offset()).show();
	},function(){
		$(this).next('.tooltip.new').hide();
	});

	// tooltip iStore
	$('.tooltip.iStore').before('<img src="css/img/icon-phone.gif" class="tooltip_istore" />');
	$('img.tooltip_istore').click(function(){
		var theLink = $(this).parent().find("a").attr("href");
		if (theLink) {
			window.location = theLink;
			return true;
		}
		return false;
	}).focus(function(){
        var pp = $(this).offset().left;
        if (pp < 600) {
            $(this).next('.tooltip.iStore').css("margin", "20px 0 0 30px");
        } else {
            $(this).next('.tooltip.iStore').css("margin", "20px 0 0 -330px");
        }
		$(this).next('.tooltip.iStore').css($(this).offset()).show();
		return false;
	}).blur(function(){
		$(this).next('.tooltip.iStore').hide();
		return false;
	}).hover(function(){
        var pp = $(this).offset().left;
        if (pp < 600) {
            $(this).next('.tooltip.iStore').css("margin", "20px 0 0 30px");
        } else {
            $(this).next('.tooltip.iStore').css("margin", "20px 0 0 -330px");
        }
		$('p.iStore:visible').hide();
		$(this).next('.tooltip.iStore').css($(this).offset()).show();
	},function(){
		$(this).next('.tooltip.iStore').hide();
	});

	// fancybox and overlays
	$("#jsonFormClose").click(function(){
		$.fancybox.close();
		return false;
	});
	$("body").delegate("#jsonFormClose2", "click", function(){
		$.fancybox.close();
		return false;
	});

	if ($('#show_lang').length){
		$("#show_lang").fancybox({
			'autoDimensions'		: false,
			'modal'					: 'true',
			'width'					: 450,
			'height'				: 'auto',
			'padding'				: 0,
			'hideOnOverlayClick'	: 'false',
			'showCloseButton'		: 'true',
			'enableEscapeButton'	: 'true',
			'titlePosition'			: 'inside',
			'scrolling'				: 'false',
			'transitionIn'			: 'fade',
			'transitionOut'			: 'none',
	
			'href'					: '#inline1',
			'onStart'				: function(){
											$('#fancybox-close').css('display','none');
									}
		});
	};

	if ($('#show_contact').length){		
		$("#show_contact").fancybox({
			'autoDimensions'		: 'false',
			'modal'					: 'true',
			'width'					: 600,
			'height'				: 'auto',
			'padding'				: 0,
			'hideOnOverlayClick'	: 'false',
			'showCloseButton'		: 'true',
			'enableEscapeButton'	: 'false',
			'titlePosition'			: 'inside',
			'scrolling'				: 'false',
			'transitionIn'			: 'fade',
			'transitionOut'			: 'none',
	
			'href'					: '#inline2',
			'onStart'				: function(){
											$('#fancybox-close').css('display','block');
											$('#jsonForm').css('display','block');
											$('#jsonForm').clearForm();
											$('#mess').css('display','none');
											$('#mess').css('display','none');
											var validator = $("#jsonForm").validate();
											validator.resetForm();
											loadForm($("#inquery_type option:selected").val());
											$.fancybox.resize();
											adjustFancybox();
									}
		});
	};
	if ($('#show_login').length){
		$("#show_login").fancybox({
			'autoDimensions'		: false,
			'modal'					: 'true',
			'width'					: 450,
			'height'				: 'auto',
			'padding'				: 0,
			'hideOnOverlayClick'	: 'false',
			'showCloseButton'		: 'true',
			'enableEscapeButton'	: 'true',
			'titlePosition'			: 'inside',
			'scrolling'				: 'false',
			'transitionIn'			: 'fade',
			'transitionOut'			: 'none',
	
			'href'					: '#inline3',
			'onStart'				: function(){
											$('#fancybox-close').css('display','none');
									}
		});
	};
	if ($('#show_store').length){
		$("#show_store").fancybox({
			'autoDimensions'		: false,
			'modal'					: 'true',
			'width'					: 450,
			'height'				: 'auto',
			'padding'				: 0,
			'hideOnOverlayClick'	: 'false',
			'showCloseButton'		: 'true',
			'enableEscapeButton'	: 'true',
			'titlePosition'			: 'inside',
			'scrolling'				: 'false',
			'transitionIn'			: 'fade',
			'transitionOut'			: 'none',
	
			'href'					: '#inline4',
			'onStart'				: function(){
											$('#fancybox-close').css('display','none');
											$('#fancybox-close').css('display','none');
									}
		});
	};
	// toggle options for search
	$('#search_options .close').click(function(){
		$('#search_global_form #search_query').val($('#search_options_form #search_query').val());
		$("#search_options").addClass('hide');
		return false;
	});

    $('#s_options').click(function() {
		var div_form = $('#search_options');
		if (div_form.hasClass('hide')) {
			$('#search_options_form #search_query').val($('#search_global_form #search_query').val());
			div_form.removeClass('hide');
		} else {
			$('#search_global_form #search_query').val($('#search_options_form #search_query').val());
			div_form.addClass('hide');
		}
		return false;
    });

	// validate search
	jQuery.validator.addMethod("checkQuery", function(theQuery) {
		if (theQuery == "Keyword(s)") {
			return false;
		} else {
			return true;
		}
	}, "");

	$("#search_global_form").validate({
		rules: {
			search: {
				required: true,
				minlength: 3,
				checkQuery: true
			}
		},
		messages: {
			search: ""
		}
	});
	$("#search_options_form").validate({
		rules: {
			search: {
				required: true,
				minlength: 3,
				checkQuery: true
			}
		},
		messages: {
			search: ""
		}
	});

	// toggle options in options for search
	// NB there is a bugg in this function in jQuery http://api.jquery.com/change/ the change don't work on IE using keyboard. 
	if ($('#tab').val() == "1") {
		$("#search_options_hidden").show();
	} else {
		$("#search_options_hidden").hide();
	}
	$('#tab').change(function () {
		if ($('#tab').val() == "1") {
			$("#search_options_hidden").show();
		} else {
			$("#search_options_hidden").hide();
		}
	});

	// setting all emtyp input fields in products shop to "Qty"
	$('.buy input').each(function(i){
		if ($(this).val() == '') {
			$(this).val('Qty');
		}
	});

	// controlling the text for "Qty" in products shop
	$('.buy input').focus(function(){
		if ($(this).val() == 'Qty') {
			$(this).val('');
		}
	}).blur(function(){
		if ($(this).val() == '') {
			$(this).val('Qty');
		}
		return false;
	});

	// close success message
	$("#message").animate({opacity: 1.0}, 5000).fadeOut("slow");

	// center shop message
	$("#shop_message").center();

	// center shop message
	$("#shop_error_large").center();

	// close shop message
	setTimeout(function() {
    	$("#shop_message .success").parent().hide();
	}, 3000);

	$("input.toggleCheck").click(function() {
		var toggleVal = '#' + $(this).val();
    	if (this.checked == true) {
    		$(this).parent().parent().parent().find(toggleVal).attr("disabled", false);
		} else {
    		$(this).parent().parent().parent().find(toggleVal).attr("disabled", true).val("");
		}
	});

	// validate contact form
	jQuery.validator.addMethod("nphone", function(phonenumber) {
		var stripped = phonenumber.replace(/[\(\)\.\-\ ]/g, '');
		if (isNaN(parseInt(stripped)) || !(stripped.length > 5)) {
			if ($('#phone').val() == ''){
				return true;			
			} else {
				return false;
			}
		} else {
			return true;
		}
	}, "!");

	var validator = $("#jsonForm").validate({
		// http://docs.jquery.com/Plugins/Validation/
		// http://bassistance.de/jquery-plugins/jquery-plugin-validation/
		rules: {
			name:	{ required: true, minlength: 2, maxlength: 5 },
			email: {
				required: "#email:filled",
				required: {
			        depends: function(element){
			            return $('#phone').val() == '';
			        }
			    },
			    email: true
			},			
			phone: {
				required: "#phone:filled",
				required: {
			        depends: function(element){
			            return $('#email').val() == '';
			        }
			    },
			    nphone: true
			}
		},
		messages: {
			name: {
				required: "!",
				minlength: jQuery.format("!")
			},
			email: {
				required: "!",
				minlength: "!",
				email: "!"
			},
			phone: {
				required: "!",
				minlength: "!"
			}			
		},
		submitHandler: function(form) {
			jQuery(form).ajaxSubmit({
				dataType: 'json',
				success: processJsonSuccess
			});
		}
	});

	// finds all tables with class pages, if there is less than 10 rows in the table we remove the paging function. 
	$("table.pages").each(function(i){
		var rowCount = $(this).find('tr').length;
		if(rowCount < 10) {
			$(this).removeClass('pages');
			$(this).next("#pager").hide();
		}
	});

	// sortable tables...
	// $.tablesorter.defaults.sortList = [[0,0]];
	$("table.sortable").tablesorter({
		widthFixed: true,
		widgets: ['zebra'],
		textExtraction: function (node) {
			return node.getAttribute('sortvalue') || node.innerHTML;
		}
	});

	// sortable table + pager...
	$("table.sortable.pages").tablesorterPager({
		widthFixed: true,	
		container: $("#pager"),
		positionFixed: false,
		textExtraction: function (node) {
			return node.getAttribute('sortvalue') || node.innerHTML;
		}
	});

	// tabs
	$('#tabs-ajax').tabs({
	show: function(event, ui) {
			if(ui.index == 0) {
				$('.tab-content').removeClass('ui-tabs-hide');
	        }
	    }
	});

	// the zoom for pro images...
	var optionsZoom =
	{
		zoomWidth: 360,
		zoomHeight: 360,
		showEffect:'show',
		hideEffect:'fadeout',
		fadeoutSpeed: 'slow',
		title :false,

		position : 'right',
		yOffset :0,
		xOffset :15

	}
	if($(".jqzoom").length){
		$(".jqzoom").jqzoom(optionsZoom);
	}

	// folding out text if there is a <!-- more --> in the text ... example in tpl-special.php
	if ($('.expandable').length){
		var tmpContent = $(".expandable").html();

		var tmpContent = tmpContent.replace(/<!-- more -->/gi,'<p class="right more"><a href="#" class="plus">Read more</a></p>');
		$(".expandable").html(tmpContent);
		$(".expandable").find("div").each(function(){
			$('p.more').nextAll().css('display','none');
		});
		$('.plus').click(function(){
			$(this).parent().nextAll().css('display','block');
			$(this).parent().remove();
			return false;
		});
	}

	// toggle faq...
	if ($('.faq').length){	
	    $('.faq .question .plus').click(function () {
			$(this).parent().hide();
			$(this).parent().parent().parent().find('div.answer').show();
			return false;
		});
	}

	// initialize scrollable
	$(".scrollable").find(".panel:first").addClass("firstPanel");
	$(".scrollable").scrollable({circular:true, speed:600});
	
});

// resize the crazy and wrong fancybox... 
function adjustFancybox() {

	if($.browser.msie && $.browser.version < 7){
		// IE 6
		var tmpHeigt = $('#fancybox-wrap').innerHeight() -42;
	} else if ($.browser.msie && $.browser.version < 8  && $.browser.version > 6){
		// IE 7
		var tmpHeigt = $('#fancybox-wrap').innerHeight() -42;
	} else if ($.browser.msie && $.browser.version < 7){
		// IE 8	
		var tmpHeigt = $('#fancybox-wrap').innerHeight() -42;
	} else {
		// xx
		var tmpHeigt = $('#fancybox-wrap').innerHeight() -50;
	}

	$('#fancybox-wrap').css('height',tmpHeigt);
	$('#fancybox-inner').css('height',tmpHeigt);

	var dt =  new Date();
	$("#datesubmitted").val(dt.getTime());
}

// sending success message back from jSon
function processJsonSuccess(data) {
	$('#jsonForm').css("display","none");
	$('#jsonForm').clearForm();
	$('#mess').css('display','block');
	var newButton = '<span class="btn"><a href="#" id="jsonFormClose2">Close</a></span>';
	$('#mess').html(data.message+newButton);
	$.fancybox.resize();
	adjustFancybox();
}

// sending error message back from jSon
function processJsonError(data) {
	$('#mess').html(data.message);
	$.fancybox.resize();
	adjustFancybox();
}

// the slider
/* OLD NOT USED
jQuery(window).bind("load", function($) {
	if (jQuery(".slider-wrap").length > 0) {
		jQuery("div.csw").prepend("<p class='loading'>Loading...</p>");
		jQuery("div#slider1").codaSlider();
		jQuery('.panel').removeAttr('title');
        var tmpURL = window.location.href;
        var tmpURL2 = jQuery('#stripNav0 a').attr('href');
        var tmpURL2 = tmpURL2.split('#')[1];

        jQuery('#stripNav0 a').attr('href','');
        jQuery('#stripNav0 a').attr('href',tmpURL+"#"+tmpURL2);
	}
});
*/

// for changing the country... 
function doIt() {
	var nc_id = $("#chCountry option:selected").val()
	$("#current_nc_id").val(nc_id);
	$("div#contactinfo").load("ajax-contactinfo?nc_id_form="+nc_id, function() {
		$.fancybox.resize();
		adjustFancybox();
	});
}

// for changing in form for contact...
function loadForm() {
	var formurl = $("#inquery_type option:selected").val()
	$("label#labelContactText").load('message_'+formurl,function() {

	});

	var ptype = $("div#producttypeinquiry");
	if(formurl.indexOf('job')>-1 || formurl.indexOf('press')>-1) {
		$("#product_type").val('P');
		ptype.hide();
	} else  {
		ptype.show();
	}

	if(formurl.indexOf('job')>-1 || formurl.indexOf('press')>-1 || formurl.indexOf('general')>-1) {
		$("#identification").hide();
	} else {
		$("#identification").show();
	}
	$("div#identification").load(formurl, function() {
		$.fancybox.resize();
		adjustFancybox();
	});
} 
function loadForm2(formurl) {
	$("#identification").hide();
	var ptype = $("div#producttypeinquiry");
	ptype.show();

	$("label#labelContactText").load('message_'+formurl,function() {
	});

	$("div#identification").load(formurl, function() {

		var tmpHeigt = $('#fancybox-wrap').innerHeight() -30;
	
		$('#fancybox-wrap').css('height',tmpHeigt);
		$('#fancybox-inner').css('height',tmpHeigt);

		$.fancybox.resize();
		adjustFancybox();
	});

}

// validate quantity in products shop
function TestDataCheck(id) {   
	var value = parseInt($(id).val());
	var IsInteger = /^\d+$/.test($(id).val());
	var valError = $(id).parent().find('[name=valid_error]').val();

	if(!IsInteger) {
		$("#shop_message").remove();
		$(id).parent().append('<div id="shop_message"><div class="error"><p>' + valError + '</p></div></div>');
		$("#shop_message").center();
		return false;
	}
	return true;
}
jQuery.fn.center = function () {
	this.css("position","absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}
