$(function () {

	$(".tabkeyExpendableHome tr").click(function () {
		var btn_lnk = $(this).find("a").attr("href");
		document.location.href=btn_lnk;
	//$(this).find("a").click();
	});

	// Fix cross browser issues if there are any
	fixCrossBrowser();

	// Configure slides for features
	if (typeof $("#bannerSlide") != 'undefined'){
		if ($("#bannerSlide > div").size() > 1) {
			var cnt = String($("#bannerSlide > div").size());
			$("#bannerAnchorContainer").addClass("bannerAnchorContainerBg" + String(cnt));

			$("#bannerSlide").cycle({
				timeout: 6000,
				pager: '#bannerSlideAnchorBox',
				pagerAnchorBuilder: function (idx, slide) {
					return '<div class="slideAnchor"></div>';
				}
			});
		}
		else {
			$("#bannerSlide > div").show();
			$(".bannerSlideAnchorBoxOut").hide();
		}
	}

	// Configure slides for breaking news
	if (typeof $("#newsSlide") != 'undefined'){
		$("#newsSlide").cycle({
			timeout: 6000,
			fx: "fade"
		});
	}
	// Configure slides for content images
	if (typeof $("#imageSlide") != 'undefined'){
		if ($("#imageSlide .imgBox").length > 3)
		{
			$("#imageSlide").cycle({
				timeout: 6000,
				pager: '#imageSlideAnchorBox',
				pagerAnchorBuilder: function (idx, slide) {
					return '<div class="slideAnchor"></div>';
				}
			});
		}
		else
		{
			$("#imageSlide div").show();
		}
	}

	// Configure slides for content images
	if (typeof $("#featureSlide") != 'undefined'){
		$("#featureSlide").cycle({
			timeout: 6000,
			pager: '#featureSlideAnchorBox',
			pagerAnchorBuilder: function (idx, slide) {
				return '<div class="slideAnchor"></div>';
			}
		});
	}

	// Configure slides for product features slide 1
	if (typeof $("#prodFeatureSlideOne") != 'undefined')
	{
		$("#prodFeatureSlideOne").cycle({
			timeout: 6000,
			pager: '#prodFeatureSlideOneAnchorBox',
			pagerAnchorBuilder: function (idx, slide) {
				return '<div class="slideAnchor"></div>';
			}
		});
	}
	if (typeof $("#prodFeatureSlideTwo") != 'undefined')
	{
		$("#prodFeatureSlideTwo").cycle({
			timeout: 6000,
			pager: '#prodFeatureSlideTwoAnchorBox',
			pagerAnchorBuilder: function (idx, slide) {
				return '<div class="slideAnchor"></div>';
			}
		});
	}
	//$("#testimonialSlides").cycle({
	//    timeout: 6000
	//});

	//$("#testimonialSlides > div").each(function () {
	// Code to handle the placement of testimonials bottom quote
	//    $('.tm>div', this).append('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
	//});
	if (typeof $("#compImgSlider") != 'undefined') {
		$("#compImgSlider").cycle({
			timeout: 6000
		});
	}
	// Country drop down
	var countryHovered = false;
	$(".countryLink").click(function () {
		$("#countryDropDown").css({
			"left": String($("#countryArrow").position().left - $("#countryDropDown").width() + 2) + "px"
		});
		$("#countryDropDown").fadeIn(500);
	});

	$(".countryLink").hover(function () { }, function () {
		$("#countryDropDown").delay(1000);
	//if (countryHovered == false) {
	//    $("#countryDropDown").fadeOut(500);
	//}
	});

	$("#countryDropDown").hover(function () {
		countryHovered = true;
	}, function () {
		countryHovered = false;
		$("#countryDropDown").fadeOut(500);
	});

	$("#countryDropDown a").click(function () {
		$("#countryDropDown").fadeOut(500);
	});

	// configure menus for the first time use
	setTimeout("configureMenus()", 10);

	// Main menu
	$("#nav ul li").hover(function () {
		$("a:first", this).addClass("select");
		$('.subMenu', this).css('display', 'block');
	}, function () {
		$("a:first", this).removeClass("select");
		$('.subMenu', this).css('display', 'none');
	});

	// Activates defaultText plugin by default
	$('input.defaultText').defaultText();

	configureCustomControls();
});

function fixCrossBrowser() {

	// Fixes for IE7
	if ($.browser.msie && $.browser.version == "7.0") {
		$("#featureSlide span").each(function () {
			var size = $(this).getHiddenDimensions();
			if (size.outerHeight < 26) {
				$(this).parent().css({
					"lineHeight": "26px"
				});
			}
		});

		// z-index fix for IE-7
		var zIndexCnt = 1000;
		$("#topBar div").each(function () {
			$(this).css('zIndex', zIndexCnt);
			zIndexCnt -= 1;
		});
		// fix text-indent issue for testimonials
		$(".invCommaStart, .invCommaEnd").hide().show();
	}
}

// Set menu width and locations based on current text
function configureMenus() {
	var firstcolumnWidth = getMaxWidthForColumn("#nav .firstcolumn li");
	var secondcolumnWidth = getMaxWidthForColumn("#nav .secondcolumn li");
	var thirdcolumnWidth = getMaxWidthForColumn("#nav .thirdcolumn li");
	var mainMenuWidth = firstcolumnWidth + secondcolumnWidth + thirdcolumnWidth + 40;

	$("#nav .firstcolumn").width(firstcolumnWidth);
	$("#nav .secondcolumn").width(secondcolumnWidth);
	$("#nav .thirdcolumn").width(thirdcolumnWidth);

	$(".smMenuTopLeft").width(mainMenuWidth);
	$(".smMenuBottomLeft").width(mainMenuWidth + 11);

	if($(".subMenu:first").prev().offset() != null)
		var firstMenuLeft = $(".subMenu:first").prev().offset().left;

	$(".subMenu").each(function () {
		var extraoffset = $(this).attr("extraoffset");
		var menuOffset = ($(this).prev().offset().left - firstMenuLeft);
		if (extraoffset) {
			menuOffset = menuOffset - parseInt(extraoffset);
		}

		var location = menuOffset * -1;
		location = location - (mainMenuWidth / 2) + 70;

		$(this).css({
			"left": String(location) + "px"
		});

		var menuWidth = $(this).prev().width() - 10;
		var hangerLocation = menuOffset + (mainMenuWidth / 2) - 70;
		$(".indicator", this).css({
			"left": String(hangerLocation) + "px"
		});
		$(".indicator span", this).css({
			"width": String(menuWidth) + "px"
		});
	});
}

function getMaxWidthForColumn(selector) {
	// Set max width to minimum width for the columns
	var maxWidth = 250;

	$(selector).each(function () {
		var size = $(this).getHiddenDimensions();
		if (size.outerWidth > maxWidth) {
			maxWidth = size.outerWidth;
		}
	//alert( $("span", this).html() + size.outerWidth);
	//$("span", this).append(String(maxWidth));
	});

	return maxWidth + 10;
}

function configureCustomControls() {

	// Handle styled combo-boxes
	$(".inputBoxCombo").each(function () {

		if ($(this).attr("autosize") == "true") {
			$(this).width($(".dropdown:first", this).width());
		}

		$("input:first", this).attr("readonly", "true");
		$("input:first", this).width($(this).width() - 30);
	});

	$(".inputBoxCombo").click(function () {

		if (!$(".dropdown:first", this).is(":visible")) {
			$(".dropdown").slideUp(100);
			$(".dropdown:first", this).slideDown(100);
		}
		return false;
	});

	$(document).click(function() {
		$(".dropdown").slideUp(100);
	});

	$(".dropdown a").click(function () {
		$(this).parents("li:first").siblings().removeClass("selected");
		$(this).parents("li:first").addClass("selected");
		$(this).parents(".dropdown").prevAll("input:first").val($(this).text());
		$(this).parents(".dropdown:first").slideUp(100);
		return false;
	});

	// Handle styled radio buttons
	$(".radiobuttonlist>div").click(function () {
		if ($(this).attr("value")) {
			$(this).siblings("input").val($(this).attr("value"));
		}

		$(this).siblings(".selected").removeClass("selected").addClass("unSelected");
		$(this).removeClass("unSelected").addClass("selected");
	});
}

$.fn.cycle.updateActivePagerLink = function (pager, currSlideIndex) {
	if (pager == "#bannerSlideAnchorBox" || pager == "#imageSlideAnchorBox"
		|| pager == "#featureSlideAnchorBox" || pager == "#prodFeatureSlideOneAnchorBox"
		|| pager == "#prodFeatureSlideTwoAnchorBox") {
		$(pager).find('div.selectedSlideAnchor').removeClass("selectedSlideAnchor").addClass("slideAnchor")
		$(pager).find('div').filter('div:eq(' + currSlideIndex + ')').removeClass("slideAnchor").addClass("selectedSlideAnchor")
	}

};

/* defaultText plugin */
jQuery.fn.defaultText = function () {
	var self = this;
	$('form').submit(function () {
		$(self).each(function () {
			var t = $(this);
			if (t.val() == t.attr('title') && t.hasClass('default')) {
				t.val('');
			}
		});
	});
	return $(this).blur(function () {
		var t = $(this);
		if (t.val() == '') {
			t.val(t.attr('title'));
			t.addClass('default');
		}
	}).focus(function () {
		var t = $(this);
		if (t.val() == t.attr('title') && t.hasClass('default')) {
			t.val('');
			t.removeClass('default');
		}
	}).blur();
};

// image preloading plugin
$.fn.preload = function () {
	this.each(function () {
		$('<img/>')[0].src = this;
	});
};

// hidden element dimensions plugin
(function ($) {
	$.fn.getHiddenDimensions = function (includeMargin) {
		var $item = this,
		props = {
			position: 'absolute',
			visibility: 'hidden',
			display: 'block'
		},
		dim = {
			width: 0,
			height: 0,
			innerWidth: 0,
			innerHeight: 0,
			outerWidth: 0,
			outerHeight: 0
		},
		$hiddenParents = $item.parents().andSelf().not(':visible'),
		includeMargin = (includeMargin == null) ? false : includeMargin;

		var oldProps = [];
		$hiddenParents.each(function () {
			var old = {};

			for (var name in props) {
				old[name] = this.style[name];
				this.style[name] = props[name];
			}

			oldProps.push(old);
		});

		dim.width = $item.width();
		dim.outerWidth = $item.outerWidth(includeMargin);
		dim.innerWidth = $item.innerWidth();
		dim.height = $item.height();
		dim.innerHeight = $item.innerHeight();
		dim.outerHeight = $item.outerHeight(includeMargin);

		$hiddenParents.each(function (i) {
			var old = oldProps[i];
			for (var name in props) {
				this.style[name] = old[name];
			}
		});

		return dim;
	}
} (jQuery));

/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
jQuery.cookie = function (name, value, options) {
	if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
			value = '';
			options.expires = -1;
		}
		var expires = '';
		if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
			var date;
			if (typeof options.expires == 'number') {
				date = new Date();
				date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
			} else {
				date = options.expires;
			}
			expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
		}
		// CAUTION: Needed to parenthesize options.path and options.domain
		// in the following expressions, otherwise they evaluate to undefined
		// in the packed version for some reason...
		var path = options.path ? '; path=' + (options.path) : '';
		var domain = options.domain ? '; domain=' + (options.domain) : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for (var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};

//Round Corners
$(document).ready(function () {
	if ($.fn.corner) {
		$(".textareaboxInr").corner("3px");
		$(".textareabox").corner("3px");
	}
});


