$(document).ready(function(){
	stylefix();
	newsletterModal();
	newWindow();
	launchAd();
	productAccordion();
	checkFlashEnabled();
	newsletterValidation();
	pressScroll();
//	pressAjax();
	betaFormErrorMessage();
	hdmodal();
});

this.stylefix = function()
{
	$('#second_nav li:last-child a').css('border-right','none');
}

this.hdmodal = function()
{
	$('#hd_modal').bind('click', function(){
		window.open( $(this).attr('href'), 'mywindow', 'scrollbars=0, width=761, height=738' );
		return false;
	});
	

}

this.newsletterModal = function()
{
	$('#Newsletter a[href="#close"], #HD a[href="#close"]').bind('click', function(){
		self.parent.tb_remove();
	});
	
	
}

this.hdclose = function() {
    $('#HD a[href="#close"]').bind('click', function() {
        self.parent.tb_remove();
    });
}

this.newWindow = function()
{
	$('a.new_window').bind('click', function(){
		window.open($(this).attr('href'));
		return false;
	});
}

this.launchAd = function()
{
	$('a.launch_ad').bind('click', function(){
		window.open($(this).attr('href'));
		return false;
	});
}

this.productAccordion = function()
{
	$('#more_features').accordion({
		autoHeight: false,
		collapsible: true,
        active: 'none'
	});
}

this.checkFlashEnabled = function()
{
	var version = getFlashVersion().split(',').shift(); 
	if(version > 0)
	{
		$('#unicast_home').css('background-image','url(/app_themes/unicast/images/banners/bg_home_straight_1370.jpg)');
		$('#unicast_home h1').css('display', 'none');		
	}
	else
	{
		$('#unicast_home').css('background-image','url(/app_themes/unicast/images/banners/banner_static.jpg)');
	}
}

this.newsletterValidation = function()
{
	$('#Newsletter input[type="submit"]').bind('click', function(){
		var emailRegExp = new RegExp(/^(([A-Za-z0-9]+_)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/);
		var valid = emailRegExp.test($('#Newsletter #email_box input').val().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, ''));

		if($('#Newsletter #first_name_box input').val().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '') == "" || $('#Newsletter #last_name_box input').val().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '') == "" || $('#Newsletter #email_box input').val().replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '') == "" || !valid)
		{
			alert('All fields are required.');
			return false;
		}
		else
		{
			self.parent.tb_remove();
		}
	});
}

function getFlashVersion()
{
	// ie 
	try
	{ 
		try
		{ 
			// avoid fp6 minor version lookup issues 
			// see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/ 
			var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6'); 
			
			try 
			{ 
				axo.AllowScriptAccess = 'always';
			}
			catch(e)
			{
				return '6,0,0';
			}
		}
		catch(e)
		{
		
		} 
		
		return new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1]; 
		// other browsers 
	}
	catch(e)
	{ 
		try
		{
			if(navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)
			{
				return (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1]; 
			} 
		}
		catch(e)
		{
		
		} 
	} 
	return '0,0,0'; 
}

this.pressScroll = function() {
	if($('body').attr('id') != 'Press')
	{
		return false;
	}
	
    $('#presslistyears').show();
    $('#yearlabel').bind('click', function() {
        return false;
    });
    
    $('#yearlabel').text($('#presslistyears li ul li:first-child a').text());
    $('#presslistyears li ul li a').bind('click', function() {
    $('#yearlabel').text($(this).attr('href').replace('#', ''));
    
        scrollDiv('a[name="' + $(this).attr('href') + '"]');
        return false;
    });

    scrollDiv('#presslist ul li.active');
}

this.scrollDiv = function(sel)
{
	$('#presslist').scrollTo($(sel), 800);
}

this.pressAjax = function()
{
	$('#presslist ul li a').bind('click', function(){
		if($(this).parent().hasClass('active'))
			return false;
			
		var url = $(this).attr('href');
		var id = url.replace('/about/press.aspx?id=', '');
		
		$('#presslist ul li').removeClass('active');
		$(this).parent().addClass('active');
		scrollDiv($(this));
		loadPress(id);
		
		return false;
	});
}

this.loadPress = function(id){
	$.getFeed({
		url: 'http://unicastreview.springbox.com/RSS.aspx',
		success: function(feed){
			$.each(feed.items, function(i){
				if(this.id === 'http://www.unicast.com/about/press.aspx?id=' + id)
				{
					var date_split = this.updated.split(' ');
					$('#single_press_release').empty().append('<div class="date">' + date_split[2] + ' ' + date_split[1] + ', ' + date_split[3] + '</div><div class="title">' + this.title + '</div><div class="subtitle">' + this.category + '</div><div class="pressBottom"></div><div class="release">' + this.description + '</div>');
					return false;
				}
			});
		}
	});
}

this.betaFormErrorMessage = function(){
	var clone = $('#AdKit .ErrorLabel').clone();

	if(clone.text() != '')
	{
		$('#AdKit .ErrorLabel').remove();
		$('#AdKit .form').append('<li><div id="error"><h4>Can You Clarify?</h4><p>The information you\'ve submitted may be incomplete. Please check the form and try once more. If you continue to encounter an error, please do not hesitate to call us at <strong>212-201-0800</strong>.</p></div></li>');
	}
}