function IE(){return (jQuery.browser.msie)}
function IE6(){return (jQuery.browser.msie  && (Math.floor(jQuery.browser.version) == 6))}
function Opera(){return (jQuery.browser.opera)}

function getFullHeight(el)
{
	return el.height() + (parseInt(el.css('paddingTop'))||0) + (parseInt(el.css('paddingBottom'))||0) + (parseInt(el.css('borderTopWidth'))||0) + (parseInt(el.css('borderBottomWidth'))||0);
}

function getInnerWidth(el)
{
	return el.width();
}
function getInnerHeight(el)
{
	return el.height() + (parseInt(el.css('paddingTop'))||0) + (parseInt(el.css('paddingBottom'))||0);
}

function replaceSubmitButton(wrapper, cssClass)
{
	var submitBtn = wrapper.find('input:submit');
	var submitLink = jQuery('<a href="#" class="' + cssClass + '"><span>' + submitBtn.val() + '</span></a>');
	submitBtn.replaceWith(submitLink);
	submitLink
		.click(function()
				{
					var form = jQuery(this).parents('form');
					form.submit();
					return false;
				})
		.attr('type', 'submit');
}

function fixRoundedCorners()
{
	if(IE() || Opera())
	{
		var RoundedBoxes = jQuery('#ContentWrapper, .ContentBox');
		RoundedBoxes
			.each(function()
				{
					var box = jQuery(this);
					box.css({
							position: 'relative',
							width: parseInt(getInnerWidth(box)) + 'px'
						})
						.append(
								'<div class="RoundedBoxTopShadow"></div><div class="RoundedBoxBtmShadow"></div><div class="RoundedBoxRightShadow"></div><div class="RoundedBoxLeftShadow"></div><div class="RoundedBoxTR"></div><div class="RoundedBoxBR"></div><div class="RoundedBoxBL"></div><div class="RoundedBoxTL"></div>'
							);
					if (IE6())
					{
						var h = parseInt(box.height());
						//var w = parseInt(box.width());
						if(h % 2 == 1) h = h + 1;
						//if(w % 2 == 1) w = w - 1;
							
						box.css({
								height: h + 'px'//,
								//width: w + 'px'
							});
					}
				})
	}
}


function replaceSubmitButtons()
{
	//replaceSubmitButton(jQuery('form[name=Register]'), 'LinkButton');
}



function resizeFeaturedDomainLists()
{
	var wrapper = jQuery('.FeaturedDomainLists');
	if(wrapper.size() > 0)
	{
		var maxHeight = Math.max(
					 	wrapper.find('.ContentBox:eq(0)').height(), 
						wrapper.find('.ContentBox:eq(1)').height()
					);
		wrapper.find('.ContentBox')
			.each(function(index)
				{
					var cntBox = jQuery(this);
					var cntBoxHeight = cntBox.height();
					if(cntBoxHeight < maxHeight)
						{
							var heightDiff = maxHeight - cntBoxHeight;
							var minHeightProperty = IE6()?'height':'minHeight';
							
							cntBox.css(minHeightProperty, cntBox.height() + heightDiff + 'px');
						}
				})
		
	}
}

function initVideoGalleries()
{	
	jQuery(".VideoGalleryTrigger, .VideoGallery, .VideoGalleryImage")
		.each(function()
			{
				var gallery = jQuery(this);
				var d = new Date();
				var galleryOverlayId = 'VideoGallery_' + d.getTime();
				var galleryOverlay = jQuery('<div class="VideoGalleryOverlay" id="' + galleryOverlayId + '"></div>');

				galleryOverlay
					.html('<div class="contentWrap"></div><a class="prev">prev</a><a class="next">next</a><div class="progress"></div><div class="info"></div>')
					.appendTo('body');
				try{
					gallery
						.find('a')
						.overlay({
								target: galleryOverlay,
								expose: '#f1f1f1',
								top: 'center'
							}).videogallery();
				}
				catch(err){
				}
			});
}

function initPhotoGalleries()
{
	jQuery(".PhotoGallery")
		.each(function()
			{
				var gallery = jQuery(this);
				var d = new Date();
				var galleryOverlayId = 'PhotoGallery_' + d.getTime();
				var galleryOverlay = jQuery('<div class="PhotoGalleryOverlay" id="' + galleryOverlayId + '"></div>');
				
				galleryOverlay
					.html('<a class="prev">prev</a><a class="next">next</a><div class="info"></div><img class="progress" src="assets/common/img/loading.gif" />')
					.appendTo('body');
				
				gallery.find('a')
					.overlay({
						target: galleryOverlay,
						expose: '#f1f1f1',
						top: 'center'
					})
					.gallery({speed: 800});
			});
}

// checks email address for validaity
function validEmail(email) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		return false;
	}
	return true;
}

// init sell domain form validation
function initJSFormValidation()
{
	var domainForms = jQuery('form.DomainSellForm, form.RequestPriceForm');
	if(domainForms.size() >0)
	{
		validateEmailFields();
		
		domainForms.find('input[name=EmailAddress], input[name=ConfirmEmailAddress]')	
			.keyup(validateEmailFields)
			.click(validateEmailFields);
	}
}

// validates sell domain email fields
function validateEmailFields()
{
	var domainForms = jQuery('form.DomainSellForm, form.RequestPriceForm');
	var domainFormEmail = domainForms.find('input[name=EmailAddress]');
	var domainFormConfirmEmail = domainForms.find('input[name=ConfirmEmailAddress]');
	var domainFormsSubmit = domainForms.find(':submit');
	
	var validationResult = false;
	
	// email validation
	if (validEmail(domainFormEmail.val()))
		markValidField(domainFormEmail, 'valid');
	else
		markValidField(domainFormEmail, 'invalid');
	
	// confirm email validation
	if (validEmail(domainFormConfirmEmail.val()))
	{
		if(domainFormEmail.val() == domainFormConfirmEmail.val())
		{
			markValidField(domainFormConfirmEmail, 'valid');
			validationResult = true
		}
		else
			markValidField(domainFormConfirmEmail, 'invalid');
	}
	else
		markValidField(domainFormConfirmEmail, 'invalid');
	
	// form submit validation
	if (validationResult == true)
	{
		domainForms
			.unbind('submit')
			.submit(function(){return true})
			.find('[type=submit]').css('opacity', '1')
	}
	else
	{
		domainForms
			.unbind('submit')
			.submit(function(){alert('Email and/or Confirmation Email are invalid or do not match.');domainFormConfirmEmail.focus().select(); return false})
			.find('[type=submit]').css('opacity', '0.5')
	}
	
	return validationResult;
}

// visualy markes the email field by adding css classes
function markValidField(formField, newFlag){
	if(newFlag == 'valid')
		formField.removeClass('InvalidField').addClass('ValidField');
	else
		formField.removeClass('ValidField').addClass('InvalidField');
}

function initTooltips()
{
	$("#headlinesContainer ul li a[title]").tooltip({
			tip: '.tooltip', // use div.tooltip as our tooltip
			//effect: 'fade',// use the fade effect instead of the default
			fadeOutSpeed: 100,// make fadeOutSpeed similar to the browser's default
			predelay: 0,// the time before the tooltip is shown
			position: "top right",// tweak the position
			offset: [10, 0]// tweak the position
		});	
}

jQuery(function(){
	replaceSubmitButtons();
	initVideoGalleries();
	initPhotoGalleries();
	initJSFormValidation();
	initTooltips();
})


jQuery(window).load(function(){
	resizeFeaturedDomainLists();
	fixRoundedCorners();
})
