// JavaScript Document
document.write('<style type="text/css">body { display: none; }</style>');

//<![CDATA[

// Changes have been made to how the carousel works for event tracking: note the rel tag, and the affiliated linkrel attribute
var mycarousel_itemList = [
    { url: '/images/home/home-banner-ebrain.png', title: 'Email Brain', link: '/marketing/', linkrel: 'EmailBrainScroller' },
    { url: '/images/home/home_banner_info.jpg', title: '.INFO', link: '/domains/tlds_info.php', linkrel: 'INFOScroller' },	
    { url: '/images/home/home_banner_co.jpg', title: '.CO', link: '/domains/tlds_co.php', linkrel: 'COScroller' },	
    { url: '/images/home/home_banner_me.jpg', title: '.ME', link: '/domains/tlds_me.php', linkrel: 'MEScroller' },
    { url: '/images/home/domain_home_wp.jpg', title: 'WordPress Blog', link: '/hosting/blog.php', linkrel: 'WordPressScroller' },
    { url: '/images/home/mdn_block_ox.jpg', title: 'Open-Xchange', link: '/email/business.php', linkrel: 'BizEmailScroller' },
    { url: '/images/home/domain_home_hosting.jpg', title: 'Hosting', link: '/hosting/windows.php', linkrel: 'WindowsHostingScroller' },
    { url: '/images/home/domain_home_ssl.jpg', title: 'SSL', link: '/ssl/', linkrel: 'SSLScroller' },
    { url: '/images/home/domain_home_banner_us.jpg', title: '.US', link: '/domains/tlds_us.php', linkrel: 'USScroller' },
    { url: '/images/home/home_banner_asia.jpg', title: '.ASIA', link: '/domains/tlds_asia.php', linkrel: 'ASIAScroller' },
    { url: '/images/home/home_banner_mobi.jpg', title: '.MOBI', link: '/domains/tlds_mobi.php', linkrel: 'MobiScroller' },
    { url: '/images/home/home_banner_ca.jpg', title: '.CA', link: '/domains/tlds_ca.php', linkrel: 'CAScroller' }
];

/* circular loop functions */
function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt) {
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt) {
    carousel.remove(i);
};

/* auto advance functions */
function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item) {
    return '<a href="' + item.link + '" rel="' + item.linkrel + '"><img src="' + item.url + '" width="213" height="96" alt="' + item.title + '" /></a>';
};

function stristr (haystack, needle, bool) {
    // Finds first occurrence of a string within another, case insensitive  
    var pos = 0;
 
    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );    if (pos == -1){
        return false;
    } else{
        if (bool) {
            return haystack.substr( 0, pos );        } else{
            return haystack.slice( pos );
        }
    }
}

$(document).ready(function() {
	// New Check for Australian Visitors, removes US content and fills with Australian content via AJAX.
	$.get('/_template/geo-function.php', { aus_check: 'check' }, function(data) {
		if(data == 'true') {
			$('#homesplash-host-full').remove();
			$('#homemain').remove();
			$.get('/aussie.html', function(data) {
				$('div#topnav').after(data);
			});
		}
	});
	
	$('h3.domau-link a').live('click', function() {
		$(this).attr('target', '_blank');
		pageTracker._trackEvent('Australia', 'GoToDomainComAu');
	});
	
	$('style').remove();
	
    $('#mycarousel').jcarousel({
        wrap: 'circular',
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback},
        auto: 7,
        initCallback: mycarousel_initCallback
    });
	
	var AID = '';
	var PID = '';
	var campn = '';
	var referer = '';
	
	if (jQuery.url.param('AID')) { AID = jQuery.url.param('AID').replace(/[^a-zA-Z0-9]/,''); }
	if (jQuery.url.param('aid')) { AID = jQuery.url.param('aid').replace(/[^a-zA-Z0-9]/,''); }
	if (jQuery.url.param('PID')) { PID = jQuery.url.param('PID').replace(/[^a-zA-Z0-9]/,''); }
	if (jQuery.url.param('pid')) { PID = jQuery.url.param('pid').replace(/[^a-zA-Z0-9]/,''); }
	if (jQuery.url.param('campn')) { campn = jQuery.url.param('campn').replace(/[^a-zA-Z0-9]/,''); }
	if (jQuery.url.param('refid')) { referer = escape(jQuery.url.param('refid')+';'+document.referrer+';20788'); }

	if ( AID != '' || PID != '' || campn != '' || referer != '' ) {
		$('body').append('<img src="https://secure.domain.com/affiliate.php?aid='+AID+'&pid='+PID+'&url='+referer+'&src_campn='+campn+'" alt="" style="position: absolute; width: 1px; height: 1px;" />');
	}
	
	var lang = jQuery.url.param('lang');
	
	if ( lang == 'es' || lang =='es_ES' ) {
		document.location.replace('http://www.dominio.com');
	}	
	
	$('input#DomainName').focus();

	$('#homesplash-host-full').wrapInner('<div id="ul_inner"></div>');
	$('#homesplash-host-full').prepend('<a href="javascript:slide(\'left\')" class="left"></a><a href="javascript:slide(\'right\')" class="right"></a>');
	if ( $.browser.msie && $.browser.version == '7.0' ) {
		$('#homesplash-host-full a.left').css('margin-left', '-930px');
		$('#homesplash-host-full a.right').css('margin-left', '-35px');
	}
	
	if ( $.browser.msie && $.browser.version == '8.0' ) {
		$('#homesplash-host-full a.left').css('margin-left', '1px');
		$('#homesplash-host-full a.right').css('margin-left', '895px');
	}
	
	
	if ( $.browser.msie != true ) { 
	
		$('#homesplash-host-full a.left').fadeOut('fast');
		$('#homesplash-host-full a.right').fadeOut('fast');

	}
	
	$('div#lang_notice ul li + li a:first').click(function() {
		$.get('/_template/lang_functions.php',  { lang_notice: 'disable' });
		pageTracker._trackEvent('Language Bar', 'Disable');
		$('div#lang_notice').slideUp();
		return false;
	});
	
	$('#homesplash-host-full').hover( function() {
		if ( $.browser.msie != true ) {
			$('#homesplash-host-full a.left').fadeIn('fast');
			$('#homesplash-host-full a.right').fadeIn('fast');
		}
	}, function() {
		if ( $.browser.msie != true ) {
			$('#homesplash-host-full a.left').fadeOut('fast');
			$('#homesplash-host-full a.right').fadeOut('fast');	
		}
	});
	

	//options( 1 - ON , 0 - OFF)
	var auto_slide = 1;
	var hover_pause = 1;
	var key_slide = 1;

	//speed of auto slide(
	var auto_slide_seconds = 7000;

	/*move the last list item before the first item. The purpose of this is
	if the user clicks to slide left he will be able to see the last item.*/
	$('#homesplash-host-full ul li:first').before($('#homesplash-host-full ul li:last'));

	//check if auto sliding is enabled
	if(auto_slide == 1){
		/*set the interval (loop) to call function slide with option 'right'
		and set the interval time to the variable we declared previously */
		var timer = setInterval('slide("right")', auto_slide_seconds);

		/*and change the value of our hidden field that hold info about
		the interval, setting it to the number of milliseconds we declared previously*/
		$('#hidden_auto_slide_seconds').val(auto_slide_seconds);
	}

	//check if hover pause is enabled
	if(hover_pause == 1){
		//when hovered over the list
		$('#homesplash-host-full ul').hover(function(){
			//stop the interval
			clearInterval(timer)
		},function(){
			//and when mouseout start it again
			timer = setInterval('slide("right")', auto_slide_seconds);
		});

	}

	//check if key sliding is enabled
	if(key_slide == 1){

		//binding keypress function
		$(document).bind('keypress', function(e) {
			//keyCode for left arrow is 37 and for right it's 39 '
			if(e.keyCode==37){
					//initialize the slide to left function
					slide('left');
			}else if(e.keyCode==39){
					//initialize the slide to right function
					slide('right');
			}
		});

	}
	
	// Click Event Tracking
	
	// Top nav
	$('div#topnav li#nav_domains a').click( function() {
		pageTracker._trackEvent('HomePage', 'NavDomains');
	});
	$('div#topnav li#nav_hosting a').click( function() {
		pageTracker._trackEvent('HomePage', 'NavHosting');
	});
	$('div#topnav li#nav_vps a').click( function() {
		pageTracker._trackEvent('HomePage', 'NavVPS');
	});
	$('div#topnav li#nav_email a').click( function() {
		pageTracker._trackEvent('HomePage', 'NavEmail');
	});
	$('div#topnav li#nav_webdesign a').click( function() {
		pageTracker._trackEvent('HomePage', 'NavWebDesign');
	});
	$('div#topnav li#nav_security a').click( function() {
		pageTracker._trackEvent('HomePage', 'NavSSL');
	});
	$('div#topnav li#nav_marketing a').click( function() {
		pageTracker._trackEvent('HomePage', 'NavMarketing');
	});
	
	// Banners
	$('div#homesplash-host-full li.neo a').click( function() {
		pageTracker._trackEvent('HomePage', 'HostingBanner');
	});
	$('div#homesplash-host-full li.ssl a').click( function() {
		pageTracker._trackEvent('HomePage', 'SSLBanner');
	});
	$('div#homesplash-host-full li.co a').click( function() {
		pageTracker._trackEvent('HomePage', 'COBanner');
	});
	$('div#homesplash-host-full li.iw a').click( function() {
		pageTracker._trackEvent('HomePage', 'IWSBanner');
	});
	
	// Search Block
	$('form#domainsearch').submit( function() {
		pageTracker._trackEvent('HomePage', 'DomainSearch');
	});
	$('div#domainlearnmore a').click( function() {
		pageTracker._trackEvent('HomePage', 'DomainTools');
	});
	$('p#toprightlinks a').eq(1).click( function() {
		pageTracker._trackEvent('HomePage', 'DomainTransfer');
	});
	
	// Small Promos
	$('div#homemain ul.hosting li a.hosting').click( function() {
		pageTracker._trackEvent('HomePage', 'HostingPromo');
	});
	$('div#homemain ul.hosting li a.email').click( function() {
		pageTracker._trackEvent('HomePage', 'EmailPromo');
	});
	$('div#homemain ul.design li a.sitebuilder').click( function() {
		pageTracker._trackEvent('HomePage', 'SiteBuilderPromo');
	});
	$('div#homemain ul.design li a.customdesign').click( function() {
		pageTracker._trackEvent('HomePage', 'WebDesignPromo');
	});
	
	// Bottom Scrolling Promos (because of how these load, they require slightly different code - .live() should be used sparingly
	$('ul#mycarousel a').live('click', function() {
		thisEvent = $(this).attr('rel');
		pageTracker._trackEvent('HomePage', thisEvent);
	});

});

//slide function
function slide(where){

	//get the item width
	var item_width = $('#homesplash-host-full ul li').outerWidth();

	/* using a if statement and the where variable check
	we will check where the user wants to slide (left or right)*/
	if(where == 'left'){
		//...calculating the new left indent of the unordered list (ul) for left sliding
		var left_indent = parseInt($('#homesplash-host-full ul').css('left')) + item_width;
	}else{
		//...calculating the new left indent of the unordered list (ul) for right sliding
		var left_indent = parseInt($('#homesplash-host-full ul').css('left')) - item_width;

	}

	//make the sliding effect using jQuery's animate function... '
	$('#homesplash-host-full ul:not(:animated)').animate({'left' : left_indent},500,function(){

		/* when the animation finishes use the if statement again, and make an ilussion
		of infinity by changing place of last or first item*/
		if(where == 'left'){
			//...and if it slided to left we put the last item before the first item
			$('#homesplash-host-full ul li:first').before($('#homesplash-host-full ul li:last'));
		}else{
			//...and if it slided to right we put the first item after the last item
			$('#homesplash-host-full ul li:last').after($('#homesplash-host-full ul li:first'));
		}

		//...and then just get back the default left indent
		$('#homesplash-host-full ul').css({'left' : '-930px'});
	});

}

//]]>
