﻿jQuery(document).ready(function () {

	jQuery(".teasProdFly").hide();

	jQuery(".teasProdBtn_1").click(function () {
		jQuery(".teasProdFly").hide();
		jQuery(".teasProdFly_1").toggle();
	});
	jQuery(".teasProdFlyClose_1").click(function () {
		jQuery(".teasProdFly_1").toggle();
	});

	jQuery(".teasProdBtn_2").click(function () {
		jQuery(".teasProdFly").hide();
		jQuery(".teasProdFly_2").toggle();
	});
	jQuery(".teasProdFlyClose_2").click(function () {
		jQuery(".teasProdFly_2").toggle();
	});

	jQuery(".teasProdBtn_3").click(function () {
		jQuery(".teasProdFly").hide();
		jQuery(".teasProdFly_3").toggle();
	});
	jQuery(".teasProdFlyClose_3").click(function () {
		jQuery(".teasProdFly_3").toggle();
	});

	jQuery('#carousel-prev, #carousel-next, .teasCol3 .teasContText').supersleight();

	var autoplay;
	var slide_time;
	if (jQuery('#autoplay').val() != '') {
		slide_time = isNaN(jQuery('#autoplay').val()) ? 4 : jQuery('#autoplay').val();
	} else {
		slide_time = 4;
	}

	slide_time = slide_time * 1000;

	autoplay = (jQuery('.largeTeasers ul.autoplay').length === 0) ? false : true;
	slide_time = (autoplay) ? slide_time : 0; //0 sets autoplay to false;
	
	if (slide_time < 0) {
		slide_time = 0;
		make_pause = false;
		trackPrevClick = null;
		trackNextClick = null;
	} else {
		make_pause = true;
	}

	if (jQuery('#carousel li').length > 1) {
		jQuery('#carousel-prev, #carousel-next').css({'display' : 'block'});
	} else {
		jQuery('#carousel-prev, #carousel-next').css({'display' : 'none'});
		slide_time = 0;
		make_pause = false;
	}

	jQuery('#carousel').cycle({
		fx: 'scrollHorz', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
		timeout: slide_time,
		speed:  500,
		next:   '#carousel-next',
		prev:   '#carousel-prev',
		prevNextClick:	function(isNext, zeroBasedSlideIndex, slideElement) {
			v1 = (isNext) ? 'Next' : 'Previous';
			v2 = (isNext) ? 'Next' : 'Previous';

			if (typeof(_gaq) !== 'undefined') {
				_gaq.push(['_trackEvent', 'Carousel', v1, v2]);
			}
		},
		pause:	make_pause
	});

	if (typeof( window[ 'disclaimersObj' ] ) != "undefined" ) {
		disclaimersObj = disclaimersObj.replace(/\t/g, '');
		disclaimersObj = disclaimersObj.replace(/\n/g, '');
		disclaimersObj = disclaimersObj.replace(/\r/g, '');
		disclaimersObj = disclaimersObj.replace(/&lt;/g, '<');
		disclaimersObj = disclaimersObj.replace(/&gt;/g, '>');

		obj = jQuery.parseJSON(disclaimersObj);

	}

	jQuery('.popupcontact').nyroModal({
		width: 650, //default Width If null, will be calculate automatically
		height: 580
	});

/*
	jQuery('.popupcontact').click(function(e) {
		e.preventDefault();
		new_url = jQuery(this).attr('href').split('select=');
		
		jQuery.nyroModalManual({
			debug: true,
			width: 630, //default Width If null, will be calculate automatically
			height: 550,
			forceType: 'iframe',
			title: 'contactWin2',
			url: new_url[0],
			endShowContent: function (elts, settings) {
			},
			endRemove: function () {
			}
		});
		return false;
	});


	jQuery('.popupcontact').click(function() {
		contactWin = window.open(jQuery(this).attr('href'),"contactWin2", "toolbar=0, location=0, status=1, menubar=1, scrollbars=1, resizable=1,width=700,height=600");
		contactWin.focus();
		jQuery(contactWin).ready(function () {
			
			//alert(jQuery(printWin.document).contents().find('#header'));
			//alert(jQuery(contactWin.document).contents().find('.scfCheckBoxList input'));
			//alert(jQuery(this).html());
			//alert(jQuery(this));
			//jQuery(contactWin.document).contents().find('.scfCheckBoxList').hide();
		});
		return false;
	});


	if(window.name == 'contactWin2') {
		//alert('aaa');
		// do something
		checkboxes = jQuery('.scfCheckBoxList input');
		selected = jQuery(location).attr('href').split('select=');
		if (selected.length > 0) {
			jQuery.each(checkboxes, function(i, val) {
				if (i == selected[1]-1) {
					jQuery(val).attr('checked', 'checked');
				}
			});
		}
	}
*/


	if ( (typeof(_gaq) !== 'undefined') && ((jQuery('.contactFormLocation').length > 0) || (jQuery('.contactFormTopic').length > 0)) ) {
		jQuery('.scfSubmitButton').mouseup(function() {
			_gaq.push(
				['_setCustomVar', 1, 'Location', jQuery('.contactFormLocation select option:selected').text(), 2]
			);
			_gaq.push(['_trackPageview']);
			_gaq.push(
				['_setCustomVar', 2, 'Topic', jQuery('.contactFormTopic select option:selected').text(), 2],
				['_trackEvent', 'Contact form', 'Submit']
			);
		});
	}

});

function openExternalLink(url, type, target) {
	jQuery.each(obj, function(i, val) {
		if (val.cssclass == type) {
			notice = jQuery('.notice').length;
			if (notice > 0) {
				var cur = jQuery('.notice .content .wrap');
				var cur0 = jQuery('.notice .content');
				jQuery(cur).find('h2').empty().html(val.title);
				jQuery(cur).find('.text').empty().html(val.text);
				jQuery(cur).find('.notice-button a').attr('href', url);
				jQuery(cur).find('.notice-button a').attr('target', target);
			} else {
				elm = jQuery('body').append('<div class="notice"><div class="bg"></div></div>');
				elm = jQuery('.notice .bg').append('<div class="content"><div class="wrap"></div></div>');
				elm = jQuery('.notice .content').append('<a class="close" href="javascript:hideNotification()"><img alt="Close" src="/img/notice-close.png"></a>');
				elm = jQuery('.notice .content div.wrap').append('<h2>' + val.title + '</h2>');
				elm = jQuery('.notice .content div.wrap').append('<div class="text">' + val.text + '</div>');
				elm = jQuery('.notice .content div.wrap').append('<p class="notice-button"><a target="' + target +'" href="">Ok</a></p>');
				jQuery('.notice-button a').attr('href', url);
				jQuery('.notice-button a').click(function() {
					if (typeof(_gaq) !== 'undefined') {
						var cur_url = document.location.href;
						var new_url = jQuery('.notice-button a').attr('href');
						_gaq.push(['_trackEvent', 'Disclaimer', 'Exit', new_url]);
					}
					hideNotification();
				});
				prepared = false;
			}
		}
	});

	showNotification(true);

}

function prepare() {
	if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
		img = jQuery(".notice .content .close img");
		var styles = {
			'width': img.width() + 'px',
			'height': img.height() + 'px',
			'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + img.attr('src') + "', sizingMethod='scale')"
		};
		img.css(styles).attr('src', '/img/x.gif');
		prepared = true;
	}
}

// ## NOTIFICATION
function showNotification(closeable) {
	var maskHeight = jQuery(document).height();
	var maskWidth = jQuery(window).width();

	jQuery('.notice').css({'width':maskWidth,'height':maskHeight});
	jQuery(".notice").show();
	jQuery(".notice .content .close").css("display", (closeable ? 'block' : 'none'));
	
	if (!prepared) { prepare(); }
	
	updatePosition();
}

function hideNotification() {
	jQuery(".notice").hide();
}

function updatePosition() {
	var margin = (jQuery(".notice .content").height()/2)*-1;
	jQuery(".notice .content").css('margin-top', margin + 'px');
	if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7 && parseInt(jQuery.browser.version, 10) > 4) {
		jQuery(".notice .content").css('top', (jQuery(window).height()/2 + jQuery(window).scrollTop()) + 'px');
	}
	// IF TEXT CONTENT IS BIGGER THAN CLIENT HEIGHT, CHANGE IT
	if(jQuery(".notice .content").height() >= jQuery("html").height()){
		jQuery(".notice").css("position", "absolute").css("height", jQuery(".notice .content").height()+40);
	} else {
		jQuery(".notice").css("position", "fixed").css("height", "100%");
	}
}


function SubmitSearch(searchWords) {
    var query;
    query = "q=" + encodeURI(searchWords);

    strCategory = jQuery("#Categories").val();
    //document.forms[0].Categories.options[document.forms[0].Categories.selectedIndex].value
    if (typeof (strCategory) != 'undefined') { 
        if (strCategory.length !== 0) {
        
            query += "&requiredfields=Categories:" + encodeURI(strCategory);
        }
    }
    if (searchWords.length !== 0 || "Enter search term" ) {    
        document.location.href = "/search.aspx?" + query;
    }
}
function checkEnter(e, caller, query) //e is event object passed from function invocation
{
    var characterCode; //literal character code will be stored in this variable

    if (e && e.which) { //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)
    {
        //alert(document.getElementById(caller).getAttribute('onClick'));

        SubmitSearch(query);
        return false;

    }
    else {
        return true;
    }
}

/* contact form */
function getFormularDropListCustomChange(valueForShow, formID, idsForToShow){
	__doPostBack('__Page', formID);
}

function __doPostBack(eventTarget, eventArgument) {
	if (!theForm.onsubmit || (theForm.onsubmit() !== false)) {
		theForm.__EVENTTARGET.value = eventTarget;
		theForm.__EVENTARGUMENT.value = eventArgument;
		theForm.submit();
	}
}
