function pop_window(url, w, h) {
	myfocus = window.open(url, 'mywindow', 'menubar=no,scrollbars=yes,width='+w+',height='+(h+30)); 
	setTimeout('myfocus.focus()',500);
}

function hovers(obj, url) {
	obj.src = url;
}

function get_xmlhttp_object() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {    
		try {
			// Internet Explorer
			xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e) {
			xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
		}
	}
	if (xmlHttp == null) alert('Your browser does not support latest Javascript version, sorry.');
	return xmlHttp;
}

function get_file_contents(file) {
	var xmlHttp = get_xmlhttp_object();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) { // indicates completed state
			alert(xmlHttp.responseText);
			return xmlHttp.responseText;
		}
	}
	xmlHttp.open('GET', file, true);
	xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	xmlHttp.send(null);
}

function handle_subnavigation() {
	$('#subNavigation a').each(function(){
		if ($(this).parent().find('ul').length > 0) {
			if ($(this).attr('id') != 'americanEagle' && $(this).attr('id') != 'traditionSeries') {
				$(this).click(function (e) {
					e.preventDefault();
					$(this).parent().toggleClass('active');
					$(this).toggleClass('clicked');
					//alert('test');
				});
			}
		}
	});
}

function round_borders() {
	var blank = '';
	if ($.browser.msie && $.browser.version < 7) { // for ie 5.5 and 6
		//blank = '&nbsp;';
	}
	var pSet, pBody, pContent;
	$('.panel').each(function(){
		pSet = $(this);
		if (!$(this).hasClass('clear')) {

			if ($.browser.msie && $.browser.version < 7) { // for ie 5.5 and 6
				pSet.html('<div class="t">' + blank + '</div><div class="tr">' + blank + '</div><div class="tl">' + blank + '</div><div class="l"><div class="r"><div class="panelBody">' + pSet.html() + '</div></div></div><div class="bl">' + blank + '</div><div class="b">' + blank + '</div><div class="br">' + blank + '</div>');
			} else {

				pSet.html('<div class="tl">' + blank + '</div><div class="t">' + blank + '</div><div class="tr">' + blank + '</div><div class="l"><div class="r"><div class="panelBody">' + pSet.html() + '</div></div></div><div class="bl">' + blank + '</div><div class="b">' + blank + '</div><div class="br">' + blank + '</div>');

			}

		} else {
			pSet.find('h3').each(function(){
				$(this).replaceWith('<div class="panelHeader"><div class="tl"></div><div class="t"></div><div class="tr"></div><div class="l"><div class="r"><h3 class="' + $(this).attr('class') + '">' + $(this).html() + '</h3></div></div><div class="bl"></div><div class="b"></div><div class="br"></div></div>');
			});
		}
	});
	$('.panel .panelContent').each(function(){
		if (!$(this).hasClass('withoutBg')) {
			$(this).html('<div class="tl"></div><div class="tr"></div>' + $(this).html() + '<div class="bl"></div><div class="br"></div>');
		}
	});
	$('.panel h3.special').each(function(){
		$(this).html('<strong><span>' + $(this).html() + '</span></strong>');
	});
	$('.showstand .showstandThumb').each(function(){
		$(this).html('<div class="tl"></div><div class="t"></div><div class="tr"></div><div class="l"><div class="r"><div class="thumbBody">' + $(this).html() + '</div></div></div><div class="bl"></div><div class="b"></div><div class="br"></div>');
	});
	$('#subNavigation').each(function(){
		$(this).html('<div class="tl"></div><div class="t"></div><div class="tr"></div><div class="l"><div class="r"><div class="subNavigationBody">' + $(this).html() + '</div></div></div><div class="bl"></div><div class="b"></div><div class="br"></div>');
	});
	$('#subNavigation ul').each(function(){
		$(this).html('<div class="tl"></div><div class="tr"></div>' + $(this).html() + '<div class="bl"></div><div class="br"></div>');
	});
	if (navigator.userAgent.indexOf('MSIE')) {
		//
	}
}

function email_to_friend(url) {
	pop_window('/email_to_friend.php?url='+escape(url), 450, 350);
}

function print_page(pageID) {
	print();
}

function setup() {
	round_borders();
	handle_subnavigation();

	if (window.location.href.match('/map-and-directions.htm$')) { // show google map on contact page
		googlemap();
	}

	if (typeof swap_testimonials == "function") {
		swap_testimonials();
	}

	$('form').submit( function () {
		modifyForms();
	});
}

function modifyForms() {
	var extraHtml = '<input type="hidden" name="code" value="secret" />';
	$('form').append(extraHtml);
}

function adjust_heights() {
	var pSet, pBody, pContent;
	var heightSet, heightBody, heightContent = 0;
	$('.panelSet').each(function(){
		pSet = $(this);
		pBody = pSet.find('.panel .panelBody');
		pContent = pSet.find('.panel .panelContent');
		heightSet = pSet.height() - 10;
		if (pBody.css('padding-bottom') == undefined) {
			pBody.css('padding-bottom', '7px');
			pBody.css('padding-top', '7px');
		}
		heightBody = heightSet - (parseInt(pBody.css('padding-top').replace('px', '')) + parseInt(pBody.css('padding-bottom').replace('px', '')));
		heightContent = heightBody - (37 + parseInt(pContent.css('padding-bottom').replace('px', '')));
		pSet.find('.panel .panelBody').each(function(){
			$(this).height(heightBody + 'px');
		});
		pSet.find('.panel .panelContent').each(function(){
			$(this).height(heightContent + 'px');
		});
	});
}

//if (!window.location.href.match('/administration/')) {
	$(document).ready(function(){   
		setup();
		if ($.browser.msie) { // for IE
			adjust_heights();
		}
	});
	window.onload = function () {

		if (!$.browser.msie) { // all except IE
			adjust_heights();
		}
		
		if ($.browser.msie && $.browser.version < 7) { // for ie 5.5 and 6
			//$('.panel').html( $('.panel').html().replace(/[\s]+/ + '"<div class="tl"></div>' + /[\s]+/g, '') );
		}

		//$('#footer').css('top', $('#container').height() - $('#footer').height());
	};
//}









/* popup */
function popup(counter, pageID) {
	if (counter) {
		var that = this;
		that.pop = $('#popup');
		that.overlay = $('#pageOverlay');
		that.counter = counter;
		that.timer = null;
		that.position = function () {
			if (that.pop.length > 0) {
				var w = $('body').width();
				var h = $(document).height();
				var popW = pop.width();
				var popH = pop.height();
				that.pop.css({
					left: Math.floor($(window).scrollLeft() + ($(window).width() - popW) / 2) + 'px',
					top: Math.floor($(window).scrollTop() + ($(window).height() - popH) / 2) + 'px'
				});
				that.overlay.width(w);
				that.overlay.height(h);
			}
		}
		that.close = function () {
			that.overlay.fadeOut();
			that.pop.fadeOut(function () {
				that.pop.remove();
				that.overlay.remove();
			});
		}
		that.open = function () {
			var url = '/ajax/request_more_info.php';
			if (pageID) {
				$.ajax({
					type: 'GET',
					url: url,
					data: 'pageID=' + escape(pageID) + '&json=1',
					error: function(xhr, status, error) {
						alert('Ajax error has occurred.');
					},
					success: function(data) {
						var html = '<div id="pageOverlay"></div><div id="popup">' + data + '<a href="" class="close"><span>close</span></a></div>';
						if (data > '') {
							$('body').append(html);
							that.setupThings();
							that.position();
							that.overlay.fadeIn();
							that.pop.fadeIn();
						}
					}
				});
			}
		}
		that.setupThings = function () {
			that.pop = $('#popup');
			that.overlay = $('#pageOverlay');
			that.pop.find('.close').click(function (e) {
				e.preventDefault();
				close();
			});
			that.pop.find('form').submit(function (e) {
				e.preventDefault();
				var frm = $(this);
				var url = frm.attr('action');
				var vars = frm.serialize();
				var buttonSet = frm.find('.buttonSet');
				var button = frm.find('.button');
				buttonSet.html('<div class="status" style="padding: 6px;">Processing...</div>');
				$.ajax({
					type: 'POST',
					url: url,
					data: vars + '&json=1',
					dataType: 'json',
					error: function(xhr, status, error) {
						alert('Ajax error has occurred.');
					},
					success: function(data) {
						if (data.errors) {
							var errors = [];
							for (var p in data.errors) errors.push(data.errors[p]);
							alert(" - " + errors.join("\n - "));
						} else if (data.success) {
							that.pop.html(data.success);
							setTimeout(function () {
								var url = window.location.href;
								var gaExpression = 'gaAssistance=yes';
								url = (url.match(/[?]/i)) ? url + '&' + gaExpression : url + '?' + gaExpression;
								window.location.href = url;
								that.close();
							}, 1000); // 2 secs
						}
					},
					complete: function(xhr, status) {
						buttonSet.html(button);
					}
				});
			});
		}
		that.count = function () {
			setTimeout(function () {
				that.open();
			}, that.counter * 2000);
		}
		$(window).bind('resize scroll', function () {
			that.position();
		});

		that.count();
	}
};