﻿var update_left_product_finder_dropdowns;
var product_finder_init;

jQuery(document).ready(function ($) {
    /* FOOTER POSITION */
    $('#content').css('min-height', $(window).height() - $('#header').outerHeight() - $('#footer').outerHeight());

    /* HEADER USER NAV */
    var right_w = $('#header-search-form').outerWidth() + $('#user-nav-icons').outerWidth() + 50;
    var left_p = $('#user-nav').outerWidth() - $('#user-nav').width();
    $('#user-nav').width(980 - right_w - left_p);

    /* FOOTER USER NAV */
    var left_w = $('#footer-copyright').outerWidth();
    var right_p = $('#footer-bottom-links').outerWidth() - $('#footer-bottom-links').width();
    $('#footer-bottom-links').width(980 - left_w - right_p);
	
    /* HEADER MENU DROPDOWNS */
	if($('#site-nav').size() > 0) {
		$('#site-nav>li').hover(function (e) {
			if (e.type == 'mouseenter') {
				$(this).addClass('active');
				$('.dropdown', $(this)).css('top', '1px');
			} else if (e.type == 'mouseleave') {
				hide_all_regions();
				$(this).removeClass('active');
				$('.dropdown', $(this)).css('top', '-10000px');
			}
		});
		
		$('#site-nav>li').each(function (index, item) {
			var dw = $('.dropdown', item);
			if (!dw.hasClass('countries')) {
				var dwcontent = $('.dropdown>.body>.inner>.content', item);
				var tw = 0;
				$('.row .col', dwcontent).each(function (index, item) {
					var w = $(item).outerWidth();
					if (w == 0) {
						w = $(':first-child', item).outerWidth();
					}

					tw += w;
				});

				if (tw < 200) {
					tw = 200;
				}

				var offset = $(item).offset().left - $('#header').offset().left;
				if (offset + tw > 980) {
					$('.dropdown .body', item).css('margin-left', (980 - offset - tw) + 'px');
				}

				dwcontent.css('width', tw + 'px');
				dwcontent.closest('.body').css('width', (tw - 190) + 'px');
			}
		});

		if($('#site-nav .dropdown.countries').size() > 0) {
			var countries_offset = $('#site-nav .dropdown.countries').offset().left - $('#main').offset().left + $('#site-nav .dropdown.countries').outerWidth();
			if (countries_offset < $('#site-nav .dropdown.countries .body').width()) {
				$('#site-nav .dropdown.countries .body').css('right', '-' + ($('#site-nav .dropdown.countries .body').width() - countries_offset) + 'px');
			}
		}
	}
	
	/* CONTACT BOX */
	if($('.address').size() > 0) {
		$('.address .row.email').each(function(index, item) {
			var inner_width = $('.label', item).width() + $('.data', item).width();
			if($(item).width() < inner_width) {
				$('.label', item).css('display', 'none');
			}
		});
	}

	/* FORMS */
	$('.scfCheckBoxListBorder .scfRequired').each(function(index, item) {
		if($(item).css('display') == 'none') {
			$('.scfCheckBoxListLabel', $(item).parent()).after(item);
			$(item).css('display', 'block');
		}
	});
	
	$('.scfRadioButtonListBorder .scfRequired').each(function(index, item) {
		if($(item).css('display') == 'none') {
			$('.scfRadioButtonListLabel', $(item).parent()).after(item);
			$(item).css('display', 'block');
		}
	});
	
	$('.scfRequired').each(function(index, item) {
		if($(item).css('display') == 'none') {
			$('label', $(item).parent()).after(item);
			$(item).css('display', 'block');
		}
	});
	
	$('.scfCheckBoxListBorder .scfValidatorRequired').each(function(index, item) {
		if($(item).css('display') == 'none') {
			$('.scfCheckBoxListLabel', $(item).parent()).after(item);
			$(item).css('display', 'block');
		}
	});
	
	$('.scfRadioButtonListBorder .scfValidatorRequired').each(function(index, item) {
		if($(item).css('display') == 'none') {
			$('.scfRadioButtonListLabel', $(item).parent()).after(item);
			$(item).css('display', 'block');
		}
	});
	
	$('.scfValidatorRequired').each(function(index, item) {
		if($(item).css('display') == 'none') {
			$('label', $(item).parent()).after(item);
			$(item).css('display', 'block');
			$('input[type="text"]', $(item).parent()).css('border-color', '#e22230');
			$('input[type="password"]', $(item).parent()).css('border-color', '#e22230');
			$('input[type="text"]', $(item).parent()).css('border-width', '2px');
			$('input[type="password"]', $(item).parent()).css('border-width', '2px');
			$('input[type="text"]', $(item).parent()).css('height', '28px');
			$('input[type="password"]', $(item).parent()).css('height', '28px');
		}
	});
	
	$('.scfCreditCardType').combobox(
		{
			animationType: 'slide',
			max_height: 115
		},
		null,
		false
	);
	
	function setDateSelectorNumberOfDaysInMonth(selector, days) {
		$('li', selector).each(function(index, item) {
			if(index >= days) {
				$(item).css('display', 'none');
				if($(item).hasClass('comboboxItemSelected')) {
					$('select', $(selector).closest('.comboboxContainer')).val(days);
					$('.comboboxValueContent', $(selector).closest('.comboboxContainer')).html(days);
					$(item).removeClass('comboboxItemSelected');
					$('li:eq(' + (days-1) + ')', selector).addClass('comboboxItemSelected');
				}
			} else {
				$(item).css('display', 'block');
			}
		});
	}
	
	function daysInMonth(month, year) {
		return new Date(year, month, 0).getDate();
	}
	
	function updateDate(e, item) {
		var date_selector = $(item).closest('.scfDateSelectorGeneralPanel');
		var day_selector = $('.comboboxContainer.date .comboboxDropDownContainer ul:last', date_selector);
		var month_selector = $('.comboboxContainer.date.month .comboboxDropDownContainer ul:first', date_selector);
		var year_selector = $('.comboboxContainer.date .comboboxDropDownContainer ul:first', date_selector);
		
		var year = $('li.comboboxItemSelected', year_selector).html();
		if(year == null) year = (new Date().getFullYear());
		var month = $('li', month_selector).index($('li.comboboxItemSelected', month_selector)) + 1;
		var days = daysInMonth(month, year);
		
		setDateSelectorNumberOfDaysInMonth(day_selector, days);
	}

	
	$('.scfDateSelectorDay').combobox(
		{
			animationType: 'slide',
			max_height: 242,
			custom_class: 'date'
		},
		null,
		false
	);
	
	$('.scfDateSelectorMonth').combobox(
		{
			animationType: 'slide',
			max_height: 242,
			custom_class: 'date month'
		},
		updateDate,
		false
	);
	
	$('.scfDateSelectorYear').combobox(
		{
			animationType: 'slide',
			max_height: 242,
			custom_class: 'date'
		},
		updateDate,
		false
	);
	
	$('.scfDropList').combobox(
		{
			animationType: 'slide',
			max_height: 115
		},
		null,
		false
	);
	
	$('.scfFileUpload').wrap('<div class="scfFileUpload_wrap"></div>');
	$('.scfFileUpload_wrap').append('<span class="scfFileUpload_button">Upload</span>');
	$('.scfFileUpload_wrap').append('<span class="scfFileUpload_text"></span>');
	
	if($.browser.mozilla) {
		$('.scfFileUpload_wrap input').css('margin-left', '-251px');
	}
	
	if($.browser.msie || $.browser.opera) {
		$('.scfFileUpload_wrap input').css('width', '64px');
		$('.scfFileUpload_wrap input').css('height', '29px');
	}
	
	var fix_upload = function(e) {
		$('.scfFileUpload_text', $(e.currentTarget).parent()).html(e.currentTarget.value);
	};
	
	$('.scfFileUpload_wrap input').change(fix_upload);
	$('.scfFileUpload_wrap input').mouseout(fix_upload);

    /* TOP BIG CAROUSEL */
	var top_carousel;
    if ($('#top-carousel').size() > 0) {
        top_carousel = $('#top-carousel .slides').cycle({
            timeout: $('#carouselRotationSpeed').val() * 1,
            speed: 1000,
            prev: '#top-carousel .controls .prev',
            next: '#top-carousel .controls .next',

            before: function (currSlideElement, nextSlideElement, options, forwardFlag) {
                $('#top-carousel .controls .nav li').removeClass('selected');

                var pos = $('div.slide', top_carousel).index($(this));
                $('#top-carousel .controls .nav li').eq(pos).addClass('selected');
            }
        });

		if($.getQueryString('print') != 'true') {
			$('#top-carousel .slides').hover(function (e) {
				if (e.type == 'mouseenter') {
					top_carousel.cycle('pause');
				} else if (e.type == 'mouseleave') {
					if ($(e.relatedTarget).closest('.controls').size() == 0) {
						top_carousel.cycle('resume');
					}
				}
			});
		}

		var numSlides = $('#top-carousel .controls .nav li').size();

		var tabWidth = Math.floor(980 / numSlides);
		$('#top-carousel .controls .nav li').css('width', tabWidth + 'px');

		var lastWidth = 980 - tabWidth * (numSlides - 1);
		$('#top-carousel .controls .nav li:last').css('width', lastWidth + 'px');

		if($.getQueryString('print') != 'true') {
			$('#top-carousel .controls .nav li a').hover(function (e) {
				if (e.type == 'mouseenter') {
					var pos = $('#top-carousel .controls .nav li').index($(e.currentTarget).parent());
					top_carousel.cycle(pos);
					top_carousel.cycle('pause');
				} else if (e.type == 'mouseleave') {
					top_carousel.cycle('resume');
				}
			});
		}
    }

    /* VIDEO SPLITS */

    $('.article.video .videos').each(function (index, item) {
        var c = $('li', item).size();
        if (c == 1) {
            $('li', item).css('padding-top', (66 - $('li', item).height() / 2) + 'px');
        } else if (c == 2) {
            $('li', item).eq(0).css('padding-bottom', '16px').css('padding-top', '18px');
            $('li', item).eq(1).css('padding-top', '16px');
        }
    });

    $('.article.video .videos li a').hover(function (e) {
        if (e.type == 'mouseenter') {
            var index = $('.article.video .videos li a').index(e.currentTarget);
            $('.article.video .video').removeClass('first');
            $($('.article.video .video').get(index)).addClass('first');
        }
    });

    /* FANCYBOX VIDEO */

    $('.fancybox-video').click(function () {
		if(this.href.indexOf('youtube.com')  != -1) {
			$.fancybox({
				'padding': 0,
				'autoScale': false,
				'transitionIn': 'none',
				'transitionOut': 'none',
				'title': this.title,
				'width': 640,
				'height': 385,
				'href': '/scripts/lib/jwplayer/player.swf?file=' + this.href + '&autostart=1',
				'type': 'swf',
				'swf': {
					'wmode': 'transparent',
					'allowfullscreen': 'true'
				},
				'overlayOpacity': 0.8,
				'overlayColor': '#000000',
				'padding': 20
			});
		} else if(this.href.indexOf('hempel.23video.com')  != -1) {
			$.fancybox({
				'padding': 0,
				'autoScale': false,
				'transitionIn': 'none',
				'transitionOut': 'none',
				'title': this.title,
				'width': 640,
				'height': 385,
				'href': this.href,
				'type': 'iframe',
				'overlayOpacity': 0.8,
				'overlayColor': '#000000',
				'padding': 20
			});
		}
        return false;
    });

    /* FANCYBOX */

    $('.fancybox').click(function () {
        $.fancybox({
            'padding': 0,
            'autoScale': true,
            'transitionIn': 'none',
            'transitionOut': 'none',
            'title': this.title,
            'href': this.href,
			'overlayOpacity': 0.8,
			'overlayColor': '#000000',
			'padding': 20
        });
        return false;
    });


    /* COMBOBOX */
    if ($.fn.combobox) {
	/*** SEARCH FORM (PRODUCT FINDER) ***/

		if($('.product-finder').size() > 0) {
			var product_finder_segment;
			var product_finder_substrate;
			var product_finder_type;
			var product_finder_function;

			var segment_label = $('#search-form-dd-segment option[value="0"]').html();
			var substrate_label = $('#search-form-dd-substrate option[value="0"]').html();
			var type_label = $('#search-form-dd-type option[value="0"]').html();
			var function_label = $('#search-form-dd-function option[value="0"]').html();
			
			var product_finder_dropdown_change = function (val, elem) {
					if (val == 0) {
						$('#search-form-dd-substrate').closest('.inner').addClass('disabled');
						$('#search-form-dd-type').closest('.inner').addClass('disabled');
						$('#search-form-dd-function').closest('.inner').addClass('disabled');

						if (product_finder_substrate != undefined) {
							$('#search-form-dd-substrate option').remove();
							$('#search-form-dd-substrate').append('<option value="0" selected="selected">' + substrate_label + '</option>');
							product_finder_substrate.combobox.update();
						}

						if (product_finder_type != undefined) {
							$('#search-form-dd-type option').remove();
							$('#search-form-dd-type').append('<option value="0" selected="selected">' + type_label + '</option>');
							product_finder_type.combobox.update();
						}

						if (product_finder_function != undefined) {
							$('#search-form-dd-function option').remove();
							$('#search-form-dd-function').append('<option value="0" selected="selected">' + function_label + '</option>');
							product_finder_function.combobox.update();
						}
					} else {
						var init_selection = false;
						$('#search-form-dd-substrate option').remove();
						$('#search-form-dd-substrate').append('<option value="0" selected="selected">' + substrate_label + '</option>');
						var found = false;
						$.each(product_finder_segments, function (index, item) {
							if (item.value == val) {
								if(item.substrates != undefined) {
									found = true;
									$.each(item.substrates, function (index, substrate) {
										if(substrate.selected == '1') {
											init_selection = substrate.value;
											$('#search-form-dd-substrate option').removeAttr('selected');
											$('#search-form-dd-substrate').append('<option selected="selected" value="' + substrate.value + '">' + substrate.label + '</option>');
										} else {
											$('#search-form-dd-substrate').append('<option value="' + substrate.value + '">' + substrate.label + '</option>');
										}
									});
								}
							}
						});

						if(found) {
							$('#search-form-dd-substrate').closest('.inner').removeClass('disabled');
							if (product_finder_substrate != undefined) {
								product_finder_substrate.combobox.update();
							}
						} else {
							$('#search-form-dd-substrate').closest('.inner').addClass('disabled');
							if (product_finder_substrate != undefined) {
								$('#search-form-dd-substrate option').remove();
								$('#search-form-dd-substrate').append('<option value="0" selected="selected">' + substrate_label + '</option>');
								product_finder_substrate.combobox.update();
							}
						}
						
						
						init_selection = false;
						$('#search-form-dd-type option').remove();
						$('#search-form-dd-type').append('<option value="0" selected="selected">' + type_label + '</option>');
						found = false;
						$.each(product_finder_segments, function (index, item) {
							if (item.value == val) {
								if(item.types != undefined) {
									found = true;
									$.each(item.types, function (index, type) {
										if(type.selected == '1') {
											init_selection = type.value;
											$('#search-form-dd-type option').removeAttr('selected');
											$('#search-form-dd-type').append('<option selected="selected" value="' + type.value + '">' + type.label + '</option>');
										} else {
											$('#search-form-dd-type').append('<option value="' + type.value + '">' + type.label + '</option>');
										}
									});
								}
							}
						});
						

						if(found) {
							$('#search-form-dd-type').closest('.inner').removeClass('disabled');
							if (product_finder_type != undefined) {
								product_finder_type.combobox.update();
							}
						} else {
							$('#search-form-dd-type').closest('.inner').addClass('disabled');
							if (product_finder_type != undefined) {
								$('#search-form-dd-type option').remove();
								$('#search-form-dd-type').append('<option value="0" selected="selected">' + type_label + '</option>');
								product_finder_type.combobox.update();
							}
						}
						
						
						init_selection = false;
						$('#search-form-dd-function option').remove();
						$('#search-form-dd-function').append('<option selected="selected" value="0">' + function_label + '</option>');
						found = false;
						$.each(product_finder_segments, function (index, item) {
							if (item.value == val) {
								if(item.functions != undefined) {
									found = true;
									$.each(item.functions, function (index, func) {
										if(func.selected == '1') {
											init_selection = func.value;
											$('#search-form-dd-function option').removeAttr('selected');
											$('#search-form-dd-function').append('<option selected="selected" value="' + func.value + '">' + func.label + '</option>');
										} else {
											$('#search-form-dd-function').append('<option value="' + func.value + '">' + func.label + '</option>');
										}
									});
								}
							}
						});

						if(found) {
							$('#search-form-dd-function').closest('.inner').removeClass('disabled');
							if (product_finder_function != undefined) {
								product_finder_function.combobox.update();
							}
						} else {
							$('#search-form-dd-function').closest('.inner').addClass('disabled');
							if (product_finder_function != undefined) {
								$('#search-form-dd-function option').remove();
								$('#search-form-dd-function').append('<option value="0" selected="selected">' + function_label + '</option>');
								product_finder_function.combobox.update();
							}
						}
						
						
					}
				};
				
			if($('.product-finder-right').size() > 0) {
				product_finder_segment = $('#search-form-dd-segment').combobox(
					{
						animationType: 'slide',
						max_height: 93
					},
					product_finder_dropdown_change,
					false
				);

				product_finder_substrate = $('#search-form-dd-substrate').combobox(
					{
						animationType: 'slide',
						max_height: 93
					},
					null,
					false
				);

				product_finder_type = $('#search-form-dd-type').combobox(
					{
						animationType: 'slide',
						max_height: 93
					},
					null,
					false
				);

				product_finder_function = $('#search-form-dd-function').combobox(
					{
						animationType: 'slide',
						max_height: 93
					},
					null,
					false
				);
			} else {
				product_finder_segment = $('#search-form-dd-segment').combobox(
					{
						animationType: 'slide',
						max_height: 93
					},
					function(new_val, new_li) {
						product_finder_dropdown_change(new_val, new_li);
						$("#productlist").load('/product-list/product-search-results/ajaxresults.aspx?q=' + document.getElementById('productFinderText').value + '&segmentS=' + new_val + '&substrateS=' + document.getElementById('productFinderSubstrate').value + '&typeS=' + document.getElementById('productFinderType').value+ '&functionS=' + document.getElementById('productFinderFunction').value);
					},
					false
				);

				product_finder_substrate = $('#search-form-dd-substrate').combobox(
					{
						animationType: 'slide',
						max_height: 93
					},
					function(new_val, new_li) {
						var value = $.getQueryString('q');
						$("#productlist").load('/product-list/product-search-results/ajaxresults.aspx?q=' + document.getElementById('productFinderText').value + '&segmentS=' + document.getElementById('productFinderSegment').value + '&substrateS=' + new_val + '&typeS=' + document.getElementById('productFinderType').value+ '&functionS=' + document.getElementById('productFinderFunction').value);
					},
					false
				);

				product_finder_type = $('#search-form-dd-type').combobox(
					{
						animationType: 'slide',
						max_height: 93
					},
					function(new_val, new_li) {
						$("#productlist").load('/product-list/product-search-results/ajaxresults.aspx?q=' + document.getElementById('productFinderText').value + '&segmentS=' + document.getElementById('productFinderSegment').value + '&substrateS=' + document.getElementById('productFinderSubstrate').value+ '&typeS=' +  new_val + '&functionS=' + document.getElementById('productFinderFunction').value);
					},
					false
				);

				product_finder_function = $('#search-form-dd-function').combobox(
					{
						animationType: 'slide',
						max_height: 93
					},
					function(new_val, new_li) {
						$("#productlist").load('/product-list/product-search-results/ajaxresults.aspx?q=' + document.getElementById('productFinderText').value + '&segmentS=' + document.getElementById('productFinderSegment').value + '&substrateS=' + document.getElementById('productFinderSubstrate').value+ '&typeS=' +  document.getElementById('productFinderType').value + '&functionS=' + new_val);
					},
					false
				);
			}
			
			product_finder_init = function() {

				$('#search-form-dd-segment option').remove();
				$('#search-form-dd-segment').append('<option value="0" selected="selected">' + segment_label + '</option>');
				var init_selection = false;
				$.each(product_finder_segments, function (index, item) {
					if(item.selected == '1') {
						init_selection = item.value;
						$('#search-form-dd-segment option').removeAttr('selected');
						$('#search-form-dd-segment').append('<option selected="selected" value="' + item.value + '">' + item.label + '</option>');				
					} else {
						$('#search-form-dd-segment').append('<option value="' + item.value + '">' + item.label + '</option>');				
					}
				});
				if(init_selection != false) {
					product_finder_dropdown_change(init_selection);
				} else {
					$('#search-form-dd-substrate').closest('.inner').addClass('disabled');
					$('#search-form-dd-type').closest('.inner').addClass('disabled');
					$('#search-form-dd-function').closest('.inner').addClass('disabled');				
				}
				
				if(product_finder_segment != undefined && product_finder_segment.size() > 0) {
					product_finder_segment.combobox.update();
				}
			}
			
			product_finder_init();
		}
		
		update_left_product_finder_dropdowns = function(new_product_finder_segments) {
			product_finder_segments = new_product_finder_segments;
			product_finder_init();
		}
		
		$('.combobox.with-links select').combobox(
			{
				animationType: 'slide',
				max_height: 115
			},
			null,
			true
		);
    }
	
	$('.combobox.with-prebuilt-links').prebuilt_combobox({
		max_height: 115
	});


    /* COLLAPSIBLE LIST */
    $('.collapsible-list .toggle-all').click(function (e) {
        if ($(this).hasClass('open')) {
            $(e.currentTarget).parent().find('li').removeClass('opened');
            $(this).removeClass('open');
        } else {
            $(e.currentTarget).parent().find('li').addClass('opened');
            $(this).addClass('open');
        }

        return false;
    });

    $('.collapsible-list .toggle-item a').click(function (e) {
        $(e.currentTarget).closest('li').toggleClass('opened');
        return false;
    });


    /* TABLE LIST */

    $('.table-list-view tbody tr').each(function (index, item) {
        var max_h = 0;
        $('td .content', $(item)).each(function (index, item) {
            if ($(item).height() > max_h) {
                max_h = $(item).height();
            }
        });

        $('td .content', $(item)).height(max_h);
    });

    $('.table-list-view tbody tr').hover(function (e) {
        if (e.type == 'mouseenter') {
            $(e.currentTarget).addClass('over');
        } else if (e.type == 'mouseleave') {
            $('tr', $(e.currentTarget).closest('.table-list-view tbody')).removeClass('over');
        }
    });

    $('.table-list-view tbody tr').click(function (e) {
        var a = $('.title a', $(e.currentTarget));
        if (a.size() == 1) {
            document.location = a.attr('href');
        }
    });


    /* ADDRESS LIST */

    $('.col .address').each(function (index, item) {
        if (!$(item).hasClass('odd')) {
            if ($(item).next().hasClass('odd')) {
                var max_h = Math.max($(item).height(), $(item).next().height());
                $(item).height(max_h);
                $(item).next().height(max_h);
            }
        }
    });


    /* WORLD MAP */
    var hide_all_regions = function () {
        $('#world-map-wrapper .region').css('display', 'none');
        $('#world-map-click-image').attr('src', '/Images/wm/wm.png');

        return false;
    }

    $('#world-map area').hover(function (e) {
        if (e.type == 'mouseenter') {
            if ($('#world-map-click-image').attr('src') != '/Images/wm/wm_' + $(e.currentTarget).attr('href').replace('#region-', '') + '.png') {
                $('#world-map-hover-image').attr('src', '/Images/wm/wm_' + $(e.currentTarget).attr('href').replace('#region-', '') + '_hover.png');
            }
        } else if (e.type == 'mouseleave') {
            $('#world-map-hover-image').attr('src', '/Images/wm/wm_blank.png');
        }
    });

    $('#world-map-wrapper').click(function (e) {
        if ($(e.target).closest('.region').size() == 0 && e.target.tagName != 'a') {
            hide_all_regions();
            return false;
        }
    });

    $('#world-map area').mousedown(function (e) {
        return false;
    });

    $('#world-map area').click(function (e) {
        hide_all_regions();
        $($(e.currentTarget).attr('href')).css('display', 'block');
        $('#world-map-click-image').attr('src', '/Images/wm/wm_' + $(e.currentTarget).attr('href').replace('#region-', '') + '.png');
        $('#world-map-hover-image').attr('src', '/Images/wm/wm_blank.png');

        return false;
    });

    $('#world-map-wrapper .region .region-close').click(hide_all_regions);
	
	
	/* COLOR CARD */
	
	$('.color-card li a').click(function(e) {
		return false;
	});
	
	var big_color_card = $('<div id="big-color-card"><span class="inner"><span class="title"></span><span class="subtitle"></span></span></div>');
	big_color_card.css('display', 'none');
	$('.color-card').append(big_color_card);
	
	$('.color-card li a').hover(function (e) {
		if (e.type == 'mouseenter') {
			$('.title', big_color_card).html($('.title', e.currentTarget).html());
			$('.subtitle', big_color_card).html($('.subtitle', e.currentTarget).html());
			$('.inner', big_color_card).css('background-color', $(e.currentTarget).css('background-color'));
			big_color_card.css('top', ($(e.currentTarget).position().top - 25) + 'px');
			big_color_card.css('left', ($(e.currentTarget).position().left - 25) + 'px');
			
			if($(e.currentTarget).hasClass('white')) {
				big_color_card.addClass('white');
			} else {
				big_color_card.removeClass('white');
			}
			
			big_color_card.css('display', 'block');
		} else if (e.type == 'mouseleave') {
			if($(e.relatedTarget).closest('div[id="big-color-card"]').size() == 0) {
				big_color_card.css('display', 'none');
			}
		}
	});
	
	$('#big-color-card').hover(function (e) {
		if (e.type == 'mouseleave') {
			big_color_card.css('display', 'none');
		}
	});
	
	/* TABLE LIST */
	$('.table-list-view tbody tr').each(function (index, item) {
		var max_h = 0;
		$('td .content', $(item)).each(function (index, item) {
			if ($(item).height() > max_h) {
				max_h = $(item).height();
			}
		});
		$('td .content', $(item)).height(max_h);
	});
	
	$('.table-list-view tbody tr').hover(function (e) {
		if (e.type == 'mouseenter') {
			$(e.currentTarget).addClass('over');
		} else if (e.type == 'mouseleave') {
			$('tr', $(e.currentTarget).closest('.table-list-view tbody')).removeClass('over');
		}
	});
	
	$('.table-list-view tbody tr').click(function (e) {
		var a = $('.title a', $(e.currentTarget));
		if (a.size() == 1) {
			document.location = a.attr('href');
		}
	});
	
	/* IMAGE GALLERY */

    $('#image-gallery a.image-inner').fancybox({
		'padding': 0,
		'autoScale': true,
		'transitionIn': 'none',
		'transitionOut': 'none',
		'title': this.title,
		'href': this.href,
		'cyclic': true,
		'overlayOpacity': 0.8,
		'overlayColor': '#000000',
		'titleShow': false,
		'padding': 20,
		'showNavArrows': false,
		'onComplete': function(e) {
			$('.image-gallery-fancybox').css('min-height', $('.image-gallery-fancybox .content').outerHeight() + 'px');
			$.fancybox.resize();
		}
    });
	
	$('#fancybox-content .prev').live('click', function(e) {
		$.fancybox.prev();
		return false;
	});
	
	$('#fancybox-content .next').live('click', function(e) {
		$.fancybox.next();
		return false;
	});
	
	/* PRODUCT FINDER POSITIONING */
	if($('#productlist').size() > 0 && $('.teaser.product-finder').size() > 0) {
		var product_finder = $('.teaser.product-finder');
		var init_top = product_finder.offset().top;
		var scroll_to = 0;
		
		$(window).scroll(function(e) {
			if(init_top < $(window).scrollTop()) {
				scroll_to = $(window).scrollTop() - init_top;
			} else {
				scroll_to = 0;
			}
			
			if($(document).height() - $('#footer').outerHeight() - init_top - 30 - product_finder.outerHeight() < scroll_to) {
				scroll_to = $(document).height() - $('#footer').outerHeight() - init_top - 30 - product_finder.outerHeight();
			}
		});
		
		var scroll_timeout = 300;		
		var do_scroll = function() {
			product_finder.animate({'margin-top': scroll_to}, 200);
			setTimeout(do_scroll, scroll_timeout);
		};
		do_scroll();
	}
	
	/** pop-ups **/
	
	$('a.popup').fancybox({
		'autoScale': true,
		'width': 380,
		'height': 300,
		'transitionIn': 'none',
		'transitionOut': 'none',
		'type': 'iframe',
		'overlayOpacity': 0.8,
		'overlayColor': '#000000',
		'titleShow': false,
		'padding': 20,
		'showNavArrows': false
    });
	
	/** CLEAR FIELD **/
	
	$('.clear-field').clearField();
	
	/** PRINT **/
	
	if($.getQueryString('print') == 'true') {
		if ($('#top-carousel').size() > 0) {
			top_carousel.cycle(0);
            top_carousel.cycle('pause');
		}
		if(window.print) {
			window.print();
		}
	}
	
	/* form half-width fields spacing fix */
	$('.scfForm .halfAvailableWidth').filter(':odd').each(function(index, item) {
		if($(item).prev().hasClass('halfAvailableWidth')) {
			if($('.popupwrap').size() > 0) {
				$(item).css('padding-left', '10px');
				$(item).css('width', '185px');
				$(item).prev().css('width', '185px');
			} else {
				$(item).css('padding-left', '20px');
				$(item).css('width', '202px');
				$(item).prev().css('width', '202px');
			}
			
			if($(item).prev().height() < $(item).height()) {
				$(item).prev().height($(item).height());
			} else if($(item).prev().height() > $(item).height()) {
				$(item).height($(item).prev().height());
			}
		}
	});
	
	/* form popup height fix */
	if($('.popupwrap').size() > 0) {
		$('#content').css('min-height', 0);
		window.parent.setPopupContentHeight($('#content').outerHeight() + 10);
		
		$('.popupwrap .popup_close').click(function(e) {
			parent.jQuery.fancybox.close();
			return false;
		});
	}
});


jQuery(window).load(function () {
	/* ARTICLE IMAGES */
	jQuery('.article-image .image').each(function(index, item) {
		if(jQuery('img', item).size() > 0 && jQuery('.copyright', item).size() > 0) {
			jQuery('.copyright', item).css('top', (jQuery('img', item).height() - 20) + 'px');
		}
	});
	
	/* GALLERY IMAGES */
	jQuery('#image-gallery .image .thumb').each(function(index, item) {
		jQuery(item).css('margin-top', ((jQuery(item).parent().height() - jQuery(item).height())/2) + 'px');
	});
});

// START GIC
;(function ($) {
    $.extend({      
        getQueryString: function (name) {           
            function parseParams() {
                var params = {},
                    e,
                    a = /\+/g,  // Regex for replacing addition symbol with a space
                    r = /([^&=]+)=?([^&]*)/g,
                    d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
                    q = window.location.search.substring(1);

                while (e = r.exec(q))
                    params[d(e[1])] = d(e[2]);

                return params;
            }

            if (!this.queryStringParams)
                this.queryStringParams = parseParams(); 

            return this.queryStringParams[name];
        }
    });
})(jQuery);
// END GIC

function checkEnter(e, caller) { //e is event object passed from function invocation
    var characterCode; //literal character code will be stored in this variable

    if (e && e.which || e.which == 0) { //if which property of event object is supported (NN4)
        e = e;
        characterCode = e.which; //character code is contained in NN4's which property
    } else {
        e = event;
        characterCode = e.keyCode; //character code is contained in IE's keyCode property
    }

    if (characterCode == 13) //if generated character code is equal to ascii 13 (if enter key)
    {
        document.getElementById(caller).click();
        return false;
    } else {
        return true;
    }
}


function SubmitSearch(searchWords, start) {
    if (searchWords.length != 0 && start.length != 0) {
        location.href = "/search.aspx?q=" + encodeURI(searchWords + "&") + "start=" + start;
        return false;
    }
}

function resizeFrame(height) {
	$('#content iframe').css('height', height + 'px');
}

function setPopupContentHeight(height) {
	jQuery('#fancybox-content').css('height', height + 'px');
}

function openPrintPopUp() {

    var winName = "Print";
    var Wwidth = "985px";
    var Wheight = "700px";

    wleft = parseInt((parseInt(screen.width) - Wwidth) / 2);
    wtop = parseInt((parseInt(screen.height) - Wheight) / 2);

    var param = '';
    if (document.URL.indexOf("?") !== -1)
        param = '&print=true';
    else
        param = '?print=true';

    printWin = window.open(document.URL + param, winName, "channelmode=0, directories=1, fullscreen=0, location=0, menubar=0, resizable=0, scrollbars=1, status=1, titlebar=1, toolbar=0,width=" + Wwidth + ",height=" + Wheight + ", left=" + wleft + ", top=" + wtop);
    printWin.focus();
	
	return false;
}
