/**
 * This function creates a tag and returns it. You can then append it wherever you want it.
 */
var createTag = function(tag, attributes, content)
{
	var newTag = document.createElement(tag);
	if (content) {
		newTag.innerHTML += content;
	};
	
	for (var prop in attributes) {
		if (attributes.hasOwnProperty(prop)) {
			newTag.setAttribute(prop, attributes[prop]);
		};
	}
	
	return newTag;
}

var appendHead = function(element)
{
	var headID = document.getElementsByTagName("head")[0];         
	headID.appendChild(element);
}

var appendBody = function(element)
{
	var headID = document.getElementsByTagName("body")[0];         
	headID.appendChild(element);
}

/**
 * Adds a script tag to the body element
 */
var bodyScriptLoad = function(src, async, content)
{
	if ($('body script[src="' + src + '"]').length == 0) {
		var attributes = {'type': 'text/javascript', 'src': src};
		if (async) {
			attributes['async'] = true; 
		};
		appendBody(createTag('script', attributes, content));
	};
}

/**
 * Adds a script tag to the head element
 */
var headScriptLoad = function(src, async, content)
{
	if ($('head script[src="' + src + '"]').length == 0) {
		var attributes = {'type': 'text/javascript', 'src': src};
		if (async) {
			attributes['async'] = true; 
		};
		appendHead(createTag('script', attributes, content));
	};
}

/**
 * Adjust height of main content to left or right content boxes
 */
var adjustMainContentHeight = function()
{
    if($('#sheet_content').length) {
    
        var ref_height = $('#right_content').height() > $('#left_content').height() ? $('#right_content').height() : $('#left_content').height();
        var head_height = ($('#head_main_content').height() + $('#head_top_content').height() + $('#head_title_content').height() + ($('#head_filter_content').length ? $('#head_filter_content').height() : 0) + ($('#sheet_content_bottom').length ? $('#sheet_content_bottom').height() : 0) + ($('#sheet_content_top').length ? $('#sheet_content_top').height() : 0));
                
        if($('#box_latest_products').length){
            if(($('#sheet_content').height() + $('#box_latest_products').height() + head_height) < ref_height)
                $('#box_latest_products_content').css('margin-bottom',ref_height-$('#box_latest_products').height()-$('#sheet_content').height()-head_height);
        } else {
            if(($('#sheet_content').height() + head_height) < ref_height)
                $('#sheet_content_inside').length ? $('#sheet_content_inside').height(ref_height-head_height) : $('#sheet_content').height(ref_height-head_height);
        }
    }
}


/**
 * disable state selection if the country select has any other value
 * than 8992208 (United States of America)
 * 
 * @param DOM_Element country_select
 */
var handleCountrySelect = function (country_select) 
{
   var country_id = $(country_select).val();
   var state_select = $(country_select).closest('form').find('select[name="usr_state_id"]');
   
   if(country_id == 8992208) { // United States
       $(state_select).removeAttr('disabled'); // enable state selection
   } else {
       $(state_select).attr('disabled', 'disabled'); // disable state selection
       
       $(state_select).find('option:first').attr('selected','selected'); // deselect current state selection
   }
}


/*
 * initialises the carousel
 */
var initCarousel = function()
{
    var deviceAgent = navigator.userAgent.toLowerCase();
    
    var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
    
    if (! agentID) {
        $('.carousel').jCarouselLite({
            vertical: true,
            auto: 100,
            speed: 5000,
            visible: 13.0,
            easing: "jswing"
        });
    }    
}




$().ready(function() {
   
	/*
	 * var used for favorites dragging
	 */
	drag = false;

/************************* general ********************************************/

	$('.transparent').css('opacity', 0.9);
	
	// @todo comment
	if($('.text_expander_fam').length) {	
		$('.text_expander_fam').text_expand(); 
	}
	
	// @todo comment
	if($('.text_expander').length) {	
		$('.text_expander').text_expand(); 
	}


    /**
     * 
     */
	$('textarea').live('focus', function(){
		if (typeof($(this).data('text')) == 'undefined' || $(this).val() == $(this).data('text')) {
			$(this).data('text', $(this).val());
			$(this).val('');
		}
	});
	
	
	/**
	 * 
	 */
	$('textarea').live('blur', function(){
        if($.trim($(this).val()) == '')
		  $(this).val($(this).data('text'));
    });

	
/************************* end general ****************************************/

/************************ dealer/agent request ********************************/

    /**
     * bind submimt event on the dealer search form
     */
	$('#dea_req').bind('submit', function (e) {
		e.preventDefault();

	    var cou_id = parseInt($('#dea_cou_list').val(), 10);
	    var cty_id = parseInt($('#dea_city_search').val(), 10);
	    var zip_code = 0;
        var distance = 0;
        
        if (isNaN(cty_id)) {
        	cty_id = 0;
        };

	    if (document.dea_req.dea_zip.value) {
	        zip_code = document.dea_req.dea_zip.value.replace(" ", "");
	        
	        switch(cou_id) {
	            case 8992187: // Switzerland
	               distance = 2;
	               break;
                case 8992074: // Germany
                case 8992011: // Austria
                case 8992018: // Belgium
                   distance = 3;
                   break;
                default:
                    distance = 4;
                    break;
	        }
	    }
	    
	    location.href = $('#dea_req').attr('action') + "/" + cou_id + "/" + encodeURIComponent(zip_code) + "/" + distance + "/" + cty_id;
	});


    /**
     * bind submit event to the agent search form
     */
	$('.agt_req').bind('submit', function (e) {
		e.preventDefault();
		location.href = $(this).children('.cou_list').val();
	});

/************************ end dealer/agent request ****************************/
	
	
/************************* autocompleter & search *****************************/

    /**
     * 
     */
    $('form#pmpro > .search_str').autocomplete($('form#pmpro').attr('action'), 
					{
						//autoFill: true,
						minChars:1, 
						matchSubset:1, 
						matchContains:1, 
						cacheLength:1,
						//selectOnly:1,
						selectFirst: false,
						max: 15,
						highlight:  false
						//onItemSelect:selectItem
					}
	).result(function(event, data, formatted) {
        $(this).closest('form').submit();
    });

    /**
     *  submit when result is clicked
     */
    $('.ac_results > ul > li').livequery('click', function(){ 
         $('form#pmpro > .search_str').closest('form').submit()
    });
    
    /**
     *  submit when filter is changed
     */
    $('.filter_script').bind('change', function() {
    	  document.location.href = base_url + $(this).val();
    });
    
    /**
     * update input on cursorkey up and down
     */
	$('form#pmpro > .search_str').livequery('keyup', function(e){ 
         if(e.keyCode == 38 || e.keyCode == 40) {
            $(this).val($('.ac_results > ul > li.ac_over').html());
		 }
    });

	
	/**
	 * 
	 */
	$(".search_str").focus(function () {
		if($(this).val() == $(this).attr('title'))
		  $(this).val('');
	});
	
	
	/**
	 * 
	 */
    $(".search_str").blur(function () {
        if($(this).val() == '')
          $(this).val($(this).attr('title'));
    });
    
    
    /**
     * 
     */
	$('form.search').submit(function(e) {
		e.preventDefault();
		
		var analyticsSearchCategory = '';
		var cat = $(this).attr('id');
		var value = $('#'+cat+' > input:first-child').val();
		var value_orig = value;
		value = ascii2hex(value);
		value = encodeURIComponent(value);
		
		var params = '/';

		switch(cat) {
			case 'pmpro':
                params += hits_sts + '/' + view_sts + '/1';
				analyticsSearchCategory = 'ProMat';
			break;
			case 'nttre':
                params += '1';
				analyticsSearchCategory = 'NewTre';
            break;
			case 'dcobj':
                params += view_sts + '/1';
				analyticsSearchCategory = 'DesCol';
			break;
			case 'aipjt':
			   params += hits_sts + '/' + view_sts + '/1';
			   analyticsSearchCategory = 'ArcDes';
			break;
		}

        //virtual pageview
		if(typeof(_gaq) != 'undefined') {
		  _gaq.push(['_trackPageview', '?search=' + encodeURIComponent(value_orig) + '&category=' + analyticsSearchCategory]);	
		}

		target = base_url + cat + '/search/' + value + params;

		location.href = target;
	});


    /**
     * Convert ASCII char to hexadecimal value
     * 
     * @param str ASCII
     */
	ascii2hex = function(str)
	{
		str_new = '';
		
	    for(i  = 0; i < str.length; i++) 
		{
			if(str.charAt(i) == '#' || str.charAt(i) == '%' ||str.charAt(i) == '/' ||str.charAt(i) == '?') 
			   str_new += '0x'+dec2hex(str.charCodeAt(i));
			else
			   str_new += str.charAt(i);
	    }
		
		return str_new;
    }


    /**
     * Convert decimal to hexadecimal values
     * 
     * @param num Decimal Value
     */
    dec2hex = function(num) 
	{
		if(num.toString(16) == 0){ 
		return "00"; 
		} 
		
		return num.toString(16).toUpperCase(); 
    }


/************************ end autocompleter & search **************************/


/************************* forms general ********************************************/

    /**
     * handles a change event on an country select field
     */
    $('select[name="usr_country_id"]').live("change", function(e){
        handleCountrySelect(this);
    });

/************************* end forms general ****************************************/
   
   
/************************* account ********************************************/

    /*
     * Login
     */
    $('a.ajaxAccLogin').live('click', function(event){   
        event.preventDefault();
        $('#dialog').html('').dialog('destroy');  
        dialog($(this), 400, 240);
        return false;
    });

	/*
	 * Logout
	 */
	$('a.ajaxAccLogout').live('click', function(event){
		//$('#dialog').dialog('destroy'); 
		$('#dialog').html('').dialog('destroy');  
		dialog($(this), 400, 130);
		$('input.cancelBtn').live('click', dialogBlur);
		return false;
	});
	
    /*
     * Login Error
     */
    $('a.ajaxAccLoginError').live('click', function(event){
        //$('#dialog').dialog('destroy'); 
        $('#dialog').html('').dialog('destroy');  
        dialog($(this), 400, 250);
        return false;
    });
    
	/*
	 * Forgot Password
	 */
	$('a.ajaxAccForgotPassword').live("click", function(event){
		//$('#dialog').dialog('destroy');
		$('#dialog').html('').dialog('destroy');
		dialog($(this), 400, 170);
		return false;
	});
	
	/*
	 * Register	 
	 */
	$('a.ajaxAccRegister').live("click", function(event){
		$('#dialog').dialog('destroy');
		dialog($(this), 600, 650);
		return false;
	});
	
	/*
	 *  Edit
	 */
    $('.ajaxAccEdit').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 450);
        return false;
    });
	
    /*
     *  Profile
     */
    $('.ajaxAccProfile').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 600);
		
		$('input#ajax_newsletter').die("click").live('click', function(e){
			var self = this;
			var status = $(self).attr('checked');
			var url = $(self).closest('form').attr('action');
			
	        $.ajax({
	            type: "POST",
	            url: url,
	            data: "status=" + (status ? '1' : '0'),
	            dataType: "json",
	            success: function(result) {
	                //status nachricht in entsprechendes dom element schreiben
	                if (1 == result.status) {
	                    $(self).next('span').removeClass().addClass('success').html(result.message);
	                } else if(0 == result.status) {
	                    //$(self).attr('checked', (!status)).next('span').removeClass().addClass('error').html(result.message);
						$(self).next('span').removeClass().addClass('error').html(result.message);
	                } else if(2 == result.status) {
						$('#dialog').dialog('destroy'); 
                        dialog(target(base_url + "/account/login_error"), 400, 250);
					}
	            },
	            error: function(result){
	                //$(self).attr('checked', (!status)).next('span').removeClass().addClass('error').html('error');
	            }
	        });
			
			
			//return false;

        });
		
        return false;
    });


/**************************   admin tool **************************************/

    $('input.ajaxAdmTool').live("click", function(event){
        $.ajax( {
            type: "POST",
            url: base_url_no_lang + "admin/tool/change_admin_toolbar_status",
            dataType: "text",
            success: function(result) {
                location.reload();
            },
            error: function(result) {

            }
        });
    });
	
	
    $('input.ajaxAdmDbTurbo').live("click", function(event){
        $.ajax( {
            type: "POST",
            url: base_url_no_lang + "admin/tool/change_admin_dba_turbo_status",
            dataType: "text",
            success: function(result) {
                location.reload();
            },
            error: function(result) {

            }
        });
    });

    
	$('#ajaxAdmToolForm select').change(function(event) {	
        event.preventDefault();
		var target = $('#ajaxAdmToolForm').attr('action');
		
		 $('#ajaxAdmToolForm').ajaxSubmit({
            success: function() {
				window.location = location.href;
			}
        });

		return false;
    });

/**************************   end admin tool **********************************/

/************************* end account ****************************************/
		
/***************************** request ****************************************/

    /*
     * button fader
     */
	$('input.button_execute').live('click', function(e) {
		$(this).fadeOut();
	});

    /*
     *  Price
     */
    $('a.ajaxReqPrice').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 560);
        return false;
    });
	
    /*
     *  Tender
     */
    $('a.ajaxReqTender').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 560);
        return false;
    });
	
    /*
     *  Offer
     */
    $('a.ajaxReqOffer').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 660);
        return false;
    });
	
    /*
     *  Catalog
     */
    $('a.ajaxReqCatalogue').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 510);
        return false;
    });
	
	/**
	 * 
	 */
	$('#dialog_branch_select').livequery('change', function(){ 
        $(this).closest('form').submit();
	}); 

    /**
     * 
     */
    $('input.dialog_catalogue_request').live('click', function(e){ 
        e.preventDefault();
		var self = this;
		var target = $(self).prev('input').val();
		
		$(self).fadeOut();

		$.post(target, function(data, textStatus){
            if (data == true) {
				$(self).removeClass('dialog_catalogue_request').addClass('dialog_catalogue_request_sent');
				$(self).val($('#sent_value').val());
				$(self).die('click'); // remove on click event
				$(self).attr('onclick', null);
			}
            $(self).fadeIn();
        });
    });
	
	/**
	 * 
	 */
	$('input.dialog_catalogue_request_sent').live('click', function(e){ 
        e.preventDefault();
    });  


    /**
     * 
     */
    $('#dialog_page_nav > a').live('click', function(e){
		e.preventDefault();
        var self = this;
	    $(self).prepend('<input type="hidden" name="page_num_changed" value="1" />');
		$(self).closest('form').attr('action', $(self).attr('href')).submit();
    }); 

    /*
     *  Sample
     */
    $('a.ajaxReqSample').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 560);
        return false;
    });

    /*
     * Press
     */
    $('a.ajaxReqPress').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 530);
        return false;
    });
	
	/*
	 * Contact
	 */
    $('a.ajaxReqContact').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 530);
        return false;
    });
	
	
    /*
     * ContactPM
     */
    $('a.ajaxReqContactPm').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 650);
        return false;
    });
	
	
    /*
     * Contact About
     */
    $('a.ajaxReqContactAbt').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 410);
        return false;
    });
    
	
    /*
     * Send
     */
    $('a.ajaxReqSend').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 345);
        return false;
    });
	
	
    /*
     * Print
     */
    $('a.ajaxReqPrint').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 230);
		//add closure to preserve the anayltics clcik event on this button
		 $('input.button[name=execute]').live("click", (function (e) {
            var origOnClick = this.onclick;
            return function (e) {
                if (origOnClick != null && !origOnClick()) {
                    return false;
                }
                // do new onclick handling only if
                // original onclick returns true
                $('#dialog').dialog('destroy');
                return true;
            }
        })());
        return false;
    });
	
	
    /*
     * Qr Code
     */
    $('a.ajaxReqQr').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 400 , 360);
        return false;
    });
	
	
	/*
     * Dealer
     */
    $('a.ajaxReqDealer').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 600);
        return false;
    });
	
    /*
     * Dealer Address
     */
    $('a.ajaxReqDeaAdr').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 300);
        return false;
    });
	
	
    /*
     * Dealer AddressMap
     */
    $('a.ajaxReqDeaAdrMap').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 620);
        return false;
    });
	
    /*
     * Apply
     */
    $('a.ajaxReqApply').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 555);
        return false;
    });
    
	
    /*
     * CAD
     */
    $('a.ajaxReqCad').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 560);
        return false;
    });
	
    /*
     * CAD Download
     */
    $('a.ajaxReqCadDown').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 570);
        return false;
    });	
    
    
    /*
     * Pdf Open
     */
    $('a.ajaxPdfOpen').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 480);
        return false;
    }); 


    /*
     * Magazines & Specials called from an advertisement
     */
    $('div.ad_big > a, div.ad_small > a').live("click", function(event) {

		var href = $(this).attr('href');
		href = href.replace(/%3A/g, ':');
		href = href.replace(/%2F/g, '/');

		if(href.indexOf('magazine/db') != -1 || href.indexOf('magazine/md') != -1 || href.indexOf('magazine/wbw') != -1)
		{    
		    var target = href.substr(href.indexOf('__oadest=') + 9);
		    var link = document.createElement("a");
		    link.href = target;
		    
		    $('#dialog').dialog('destroy');
		    $('#mainopt1').live('click', function(){
		        $('.subopt:first').attr("checked", true); 
		    });
		    $('#mainopt2').live('click', function(){
		        $('.subopt').attr("checked", false);
		    });
		    $('.subopt').live('click', function(){
		        $('#mainopt1').attr("checked", true); 
		    });
		    
		    dialog(link, 600, 800);
		    return false;
		} else if(href.indexOf('special/') != -1) {
            //specials and lottery
            var target = href.substr(href.indexOf('__oadest=') + 9);

            var link = document.createElement("a");
            link.href = target;
			
			dialog(link, 600, 680);
            return false;
		}
    });
	
	
    /*
     * Specials and lottery
     */
    $('a.ajaxReqSpecial').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 680);
        return false;
    }); 
	
	
/**************************  pdf view *****************************************/

    /*
     * Pdf View
     */
    $('a.ajaxPdfView').live("click", function(event){
		
		event.preventDefault();
		
		var target = $(this).attr('href');
		var height = $(window).height();
		var width = $(window).width();
		
		window.open(target, 'showroom', 'width='+width+',height='+height); 
		
		return false;
    });
    

/************************  end pdf view ***************************************/

/************************* project map ****************************************/

    /*
     * Project Map
     */
    $('a.ajaxShowProjectMap').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 600, 550);
        return false;
    });
	
	
	
    /*
     * Project Map Big
     */
    $('a.ajaxShowProjectMapAll').live("click", function(event){
        $('#dialog').dialog('destroy');
        dialog($(this), 900, 550);
        return false;
    });


/******************************************************************************/

 
/**************************   newsletter **************************************/

/*
 * Newsletter Subscribe
 */
$('a.ajaxLetterSubscribe').live('click', function(event){   
    $('#dialog').html('').dialog('destroy');  
    dialog($(this), 600, 300);
    return false;
});

/*
 * Newsletter Unsubscribe
 */
$('a.ajaxLetterUnsubscribe').live('click', function(event){   
    $('#dialog').html('').dialog('destroy');  
    dialog($(this), 400, 200);
    return false;
});

/******************************************************************************/



/*************************** end request **************************************/
		
/************************* carousel *******************************************/		

initCarousel();

/************************* end carousel ***************************************/	
	


/************************* homepage menu fades ********************************/
    
	if(! ($.browser.msie && $.browser.version == '6.0') ) {
	
	/*
	 * Products & Materials
	 */
	$('#home_ele_nav_promat > *').hover(
		function(e){
            $('#home_ele_nav_promat').children('.home_ele_nav_ctr_head').children('a').children('img:last').hoverFlow(e.type, { 
                opacity: 1.0
            }, 500);
            $('#home_ele_nav_promat').children('.home_ele_nav_ctr').find('input').each(function (i) {
                $(this).hoverFlow(e.type, { 
				    borderBottomColor: "#fff",
					borderLeftColor: "#fff",
					borderTopColor: "#fff",
					borderRightColor: "#fff",
					color:  "#fff" 
				}, 500);
            });
			$('#home_ele_nav_promat').children('.home_ele_nav_ctr').find('.home_ele_nav_item_ctr').each(function (i) {
                $(this).hoverFlow(e.type, { 
				    color: "#fff" 
				}, 500);
			});
            $('#home_ele_nav_promat').children('.home_ele_nav_ctr').find('img.nav_arrow_ctr_on').each(function (i) {
                $(this).hoverFlow(e.type, { 
				    opacity: 1.0 
				}, 500);
            });
		},
		function(e){
            orig_color = $('#home_ele_nav_promat').css('color');
			
            $('#home_ele_nav_promat').children('.home_ele_nav_ctr_head').children('a').children('img:last').hoverFlow(e.type, { 
                opacity: 0.0 
            }, 500);
            $('#home_ele_nav_promat').children('.home_ele_nav_ctr').find('input').each(function (i) {
                $(this).hoverFlow(e.type, { 
				    borderBottomColor: orig_color,
					borderLeftColor: orig_color,
					borderTopColor: orig_color,
					borderRightColor: orig_color,
					color:  orig_color 
				}, 500);
            });		  
            $('#home_ele_nav_promat').children('.home_ele_nav_ctr').find('.home_ele_nav_item_ctr').each(function (i) {
                $(this).hoverFlow(e.type, { 
				    color: orig_color 
				}, 500);
            });
            $('#home_ele_nav_promat').children('.home_ele_nav_ctr').find('img.nav_arrow_ctr_on').each(function (i) {
                $(this).hoverFlow(e.type, { 
				    opacity: 0.0 
				}, 500);
            });
		}
	);
	
	/*
	 * News & Trends
	 */
	$('#home_ele_nav_newtre > *').hover(
		function(e){
			$('#home_ele_nav_newtre').children('.home_ele_nav_ctr_head').children('a').children('img:last').hoverFlow(e.type, { 
                opacity: 1.0 
			}, 500);
			$('#home_ele_nav_newtre').children('.home_ele_nav_ctr').find('input').each(function (i) {
				$(this).hoverFlow(e.type, { 
					borderBottomColor: "#fff",
					borderLeftColor: "#fff",
					borderTopColor: "#fff",
					borderRightColor: "#fff",
					color:  "#fff" 
				}, 500);
			});
			$('#home_ele_nav_newtre').children('.home_ele_nav_ctr').find('.home_ele_nav_item_ctr').each(function (i) {
				$(this).hoverFlow(e.type, { 
					color: "#fff" 
				}, 500);
			});
			$('#home_ele_nav_newtre').children('.home_ele_nav_ctr').find('img.nav_arrow_ctr_on').each(function (i) {
				$(this).hoverFlow(e.type, { 
					opacity: 1.0 
				}, 500);
			});
		},
		function(e){
			orig_color = $('#home_ele_nav_newtre').css('color');
			
			$('#home_ele_nav_newtre').children('.home_ele_nav_ctr_head').children('a').children('img:last').hoverFlow(e.type, { 
				opacity: 0.0 
			}, 500);
			$('#home_ele_nav_newtre').children('.home_ele_nav_ctr').find('input').each(function (i) {
				$(this).hoverFlow(e.type, { 
					borderBottomColor: orig_color,
					borderLeftColor: orig_color,
					borderTopColor: orig_color,
					borderRightColor: orig_color,
					color:  orig_color 
				}, 500);
			});         
			$('#home_ele_nav_newtre').children('.home_ele_nav_ctr').find('.home_ele_nav_item_ctr').each(function (i) {
				$(this).hoverFlow(e.type, { 
				    color: orig_color 
				}, 500);
			});  
			$('#home_ele_nav_newtre').children('.home_ele_nav_ctr').find('img.nav_arrow_ctr_on').each(function (i) {
				$(this).hoverFlow(e.type, { 
				    opacity: 0.0 
				}, 500);
			});
		}
	);
	
	/*
	 * Architecture & Interiors
	 */
	$('#home_ele_nav_arcint > *').hover(
		function(e){
			$('#home_ele_nav_arcint').children('.home_ele_nav_ctr_head').children('a').children('img:last').hoverFlow(e.type, { 
                opacity: 1.0 
			}, 500);
			$('#home_ele_nav_arcint').children('.home_ele_nav_ctr').find('input').each(function (i) {
				$(this).hoverFlow(e.type, { 
					borderBottomColor: "#fff",
					borderLeftColor: "#fff",
					borderTopColor: "#fff",
					borderRightColor: "#fff",
					color:  "#fff" 
				}, 500);
			});
			$('#home_ele_nav_arcint').children('.home_ele_nav_ctr').find('.home_ele_nav_item_ctr').each(function (i) {
				$(this).hoverFlow(e.type, { 
				    color: "#fff" 
				}, 500);
			});
			$('#home_ele_nav_arcint').children('.home_ele_nav_ctr').find('img.nav_arrow_ctr_on').each(function (i) {
				$(this).hoverFlow(e.type, { 
				    opacity: 1.0 
				}, 500);
			});
		},
		function(e){
			orig_color = $('#home_ele_nav_arcint').css('color');
			
			$('#home_ele_nav_arcint').children('.home_ele_nav_ctr_head').children('a').children('img:last').hoverFlow(e.type, { 
                opacity: 0.0 
			}, 500);
			$('#home_ele_nav_arcint').children('.home_ele_nav_ctr').find('input').each(function (i) {
				$(this).hoverFlow(e.type, { 
					borderBottomColor: orig_color,
					borderLeftColor: orig_color,
					borderTopColor: orig_color,
					borderRightColor: orig_color,
					color:  orig_color 
				}, 500);
			});         
			$('#home_ele_nav_arcint').children('.home_ele_nav_ctr').find('.home_ele_nav_item_ctr').each(function (i) {
				$(this).hoverFlow(e.type, { 
				    color: orig_color 
				}, 500);
			});  
			$('#home_ele_nav_arcint').children('.home_ele_nav_ctr').find('img.nav_arrow_ctr_on').each(function (i) {
				$(this).hoverFlow(e.type, { 
				    opacity: 0.0 
				}, 500);
			});
		}
	);  
	
	/*
	 * Designcollector
	 */
	$('#home_ele_nav_descol > *').hover(
		function(e){
			$('#home_ele_nav_descol').children('.home_ele_nav_ctr_head').children('a').children('img:last').hoverFlow(e.type, { 
                opacity: 1.0 
			}, 500);
			$('#home_ele_nav_descol').children('.home_ele_nav_ctr').find('input').each(function (i) {
				$(this).hoverFlow(e.type, { 
					borderBottomColor: "#fff",
					borderLeftColor: "#fff",
					borderTopColor: "#fff",
					borderRightColor: "#fff",
					color:  "#fff" 
				}, 500);
			});
			$('#home_ele_nav_descol').children('.home_ele_nav_ctr').find('.home_ele_nav_item_ctr').each(function (i) {
				$(this).hoverFlow(e.type, { 
                    color: "#fff" 
				}, 500);
			});
			$('#home_ele_nav_descol').children('.home_ele_nav_ctr').find('img.nav_arrow_ctr_on').each(function (i) {
				$(this).hoverFlow(e.type, { 
				    opacity: 1.0 
				}, 500);
			});
		},
		function(e){
			orig_color = $('#home_ele_nav_descol').css('color');
			
			$('#home_ele_nav_descol').children('.home_ele_nav_ctr_head').children('a').children('img:last').hoverFlow(e.type, { 
                opacity: 0.0 
			}, 500);
			$('#home_ele_nav_descol').children('.home_ele_nav_ctr').find('input').each(function (i) {
				$(this).hoverFlow(e.type, { 
					borderBottomColor: orig_color,
					borderLeftColor: orig_color,
					borderTopColor: orig_color,
					borderRightColor: orig_color,
					color: orig_color
				}, 500);
			});         
			$('#home_ele_nav_descol').children('.home_ele_nav_ctr').find('.home_ele_nav_item_ctr').each(function (i) {
				$(this).hoverFlow(e.type, { 
				    color: orig_color 
				}, 500);
			});
			$('#home_ele_nav_descol').children('.home_ele_nav_ctr').find('img.nav_arrow_ctr_on').each(function (i) {
				$(this).hoverFlow(e.type, { 
				    opacity: 0.0 
				}, 500);
			});
		}
	);

    } 
/************************* end homepage menu fades ****************************/


/****************************** dealer search *********************************/

	var city_option_list = [];
	$('#dea_cou_list').change(function(event) {
		var country_id = $(this).val();
		var cnt = 0;
		
		$('#dea_city_search option').each(function(i) {
			if ($(this).val() == '-') {
				$(this).detach();
			} else if ($(this).hasClass('country' + country_id)) {
				cnt++;
			} else {
				city_option_list.push($(this).detach());
			};
		});
		
		for (var i = 0; i < city_option_list.length; ) {
			if (city_option_list[i].hasClass('country' + country_id)) {
				$('#dea_city_search').append(city_option_list[i]);
				city_option_list.splice(i, 1);
				cnt++;
			} else {
				i++;
			}
		};
		
		if (cnt == 0) {
			$('#dea_city_search').append(createTag('option', {'value':'-'}, '-'));
			$('#dea_city_search').attr('disabled', 'disabled');
		} else {
			$('#dea_city_search').prepend(createTag('option', {'value':'-'}, ''));
			$('#dea_city_search').val($('#dea_city_search option:first').val());
			$('#dea_city_search').removeAttr('disabled');
		};
	}).change();
	
	$('input[name="dea_zip"]', $('#dea_req')).keyup(function(event) {
		if ($(this).val().length) {
			$('#dea_city_search').css('color', 'gray');
		} else {
			$('#dea_city_search').css('color', '');
		};
	}).change(function(event) {
		$(this).keyup();
	}).change();
	
	$('#dea_city_search').change(function(event) {
		$('input[name="dea_zip"]').val('').change();
	}).ready(function(event) {
		if ($('#dea_city_search option[selected="selected"]').length) {
			$('#dea_city_search').val($('#dea_city_search option[selected="selected"]').val());
		}
	});
	
	
	
	// Dealer list
	$('#dealer_country_selection select[name="cou_item"]').change(function() {
		var cou_id = $('option:selected', $(this)).val();
		var url = $('#dealer_country_selection').attr('action');
		url = url.replace('{cou_id}', cou_id);
		
		location.href = url;
	});
	
	$('#dealer_country_sub_selection select[name="area_city_item"], #dealer_country_sub_selection select[name="man_item"]').change(function() {
		var dat_id = $('option:selected', $(this)).val();
		var url = $('#dealer_country_sub_selection').attr('action');
		url = url.replace('{dat_id}', dat_id).replace(/\/[1-4]$/i, '/1');
		
		location.href = url;
	});
	
	$('#sub_filter_search_span').click(function() {
		$('#dealer_country_sub_selection').submit();
	});
	$('#dealer_country_sub_selection input[name="dea_zip"]').keyup(function(event) {
		if (event.which == 27) {
			$(this).val('');
		};
	});
	$('#dealer_country_sub_selection').submit(function(event) {
		event.preventDefault();
		var dat_id = $('input[name="dea_zip"]', $(this)).val().replace(/[^a-z0-9]/gi,'');
		var url = $('#dealer_country_sub_selection').attr('action');
		url = url.replace('{dat_id}', dat_id.length ? dat_id : 0);
		
		location.href = url;
	});
	
	

/**************************** end dealer search *******************************/


    // adjust main content height
    adjustMainContentHeight();
});

$(window).load(function() {
    // adjust main content height
    adjustMainContentHeight();
});

