// Default JS Content - Do Not Delete

var makeStyledDD = function(jQueryEl,editable,returnEvent){
	var original = $(jQueryEl);
	
	var input = $('<input type="text" name="'
			+$(original).attr('name')
			+'_" id="'
			+$(original).attr('id')
			+'_" value="'
			+original.find('option:first').text()
			+'"'
			+(editable?'':' readonly="true"')
		+'>');
	var dropDown = $('<div class="dropDown"></div>');
	
	original.hide();
	original.after(input);
	input.after(dropDown);
	dropDown.width(input.width());
	
	original.find('option').each(function(){
			dropDown.append('<div value="'+this.value+'">'+$(this).text()+'</div>');
	});
	dropDown.find('> div').hover(function(){
		$(this).addClass('selected');
	},function(){
		$(this).removeClass('selected');
	}).click(function(){
		input.val($(this).text());
		if(typeof(returnEvent)=='function') returnEvent($(this).attr('value'));
	});
	
	input.click(function(){
		this.value='';	
		dropDown.children().show();
		dropDown.slideDown(200);
		var docEvent = function(e){
			if (e.target.id!=input.attr('id')){
				dropDown.slideUp(200);
				$(document).unbind('click',docEvent);
			}
		};
		$(document).bind('click',docEvent);					
	}).keyup(function(e){
		/*
			37 left
			38 up
			39 right
			40 down
			9 tab
			13 enter
			48-90 a-z0-9
			8 backspace
			46 delete
		*/
		/* Text typed to change, letters added or deleted */
		if (dropDown.filter(':hidden').length>0) dropDown.slideDown('200');
		if( (e.which>47 && e.which<91) || e.which == 8 || e.which == 46 ){
			var i = 0;
			var regex = new RegExp('^'+this.value,'i');
			original.find('option').each(function(){
				var el = $(dropDown.children()[i]);
				if(el.text().match(regex)) el.show();
				else el.hide();
				i++;
			});
		}
		/* Arrow Up and Down */
		if( e.which == 38 || e.which == 40){
			var els = dropDown.children(':visible.selected');
			if(els.length>0){
				els.removeClass('selected');
				if(e.which == 40) els.nextAll(':visible:first').addClass('selected');
				if(e.which == 38) els.prevAll(':visible:first').addClass('selected');
			}else{
				dropDown.children().removeClass('selected');
				dropDown.children(':visible:first').addClass('selected');
			}
			input.val(dropDown.children('.selected').text());
			dropDown.stop();
			dropDown.scrollTo(dropDown.children('.selected'),1000,{offset:{top:-5},easing:'linear'});
			if(typeof(returnEvent)=='function') returnEvent(dropDown.children('.selected').attr('value'));
		}
		/* Enter or Tab */
		if( e.which == 9 || e.which == 13){
			var els = dropDown.children('.selected');
			if(els.length<0) input.val(dropDown.children(':first:visible').attr('value'));
			dropDown.slideUp(200);
		}
		
	});
};

$(document).ready(function(){
	/* Stylized Drop Downs */
	makeStyledDD('#device');
	
	/* Sign up form submission */
	$('[name=form1]').submit(function(){
		if($('#code').val()=='Enter Promo Code (optional)')
			$('#code').val('');
		$('#device').val($('.dropDown :contains('+$('#device_').val()+')').attr('value'));
		$('#device').show();
		$('#device_').hide();
		return true;
	});
	
	var defaultVal = function(field,val){
		if($('#'+field).length>0){
			$('#'+field)[0].value = val;
			$('#'+field).focus(function(){
				if (this.value==val) this.value='';
			}).blur(function(){
				if (this.value=='') this.value=val;
			});
		}
	};
	defaultVal('email', 'Enter Email Address');
	defaultVal('phone', 'Enter Phone Number*');
	defaultVal('code', 'Enter Promo Code (optional)');

  /* Question and Answer Toggles */
  $('.question').click(function(){
    $(this).next().slideToggle();
  });

  /* Small Tab Areas */
  var i = 0;
  $('.smallTabButton').each(function(){
    $(this).data('interval',i);
    i++;
  }).click(function(){
    $('.smallTabButton img').each(function(){this.src = this.src.replace(/_on\./,'.')});
    $(this).find('img').each(function(){this.src = this.src.replace(/(\.[^\.]*)$/,'_on$1');});
    $('.smallTabContent').hide();
    $('.smallTabContent:eq('+$(this).data('interval')+')').show();
  });

  /* Main Tab Areas */
  $('.tabContent').each(function(){
    $(this).parent().prev('[bgcolor=#f3f3f3]').appendTo($('.tabContentArea'));
    $(this).appendTo($('.tabContentArea'));
  });
  var i = 0;
  $('.tabButton').each(function(){
    $(this).data('interval',i);
    i++;
  }).click(function(){
	var i = $(this).data('interval');
    $('.tabButton img').each(function(){this.src = this.src.replace(/_on\./,'.')});
    $(this).find('img').each(function(){this.src = this.src.replace(/(\.[^\.]*)$/,'_on$1');});
    $('.tabContent').prev('[bgcolor=#f3f3f3]').hide();
    $('.tabContent').hide();
    $('.tabContent:eq('+i+')').prev('[bgcolor=#f3f3f3]').show();
    $('.tabContent:eq('+i+')').show()
    .find('.smallTabButton:first').click(); /* Show the first small tab in this */
	
	var w = i==0?'#iphone'
		:(i==1?'#blackberry'
		:(i==2?'#android'
		:(i==3?'#symbian'
		:(i==4?'#windows'
		:(i==5?'#pc'
		:'')))));
	document.location.href = w;
  });
  if(document.location.hash=='')
  $('.tabButton:first').click();
  
  /* Anchor Links for Tabs */
  var oHash;
  var checkHash = function(){
	  if(oHash!=document.location.hash)hashChanged();
	  setTimeout(function(){checkHash();},500);
  };
  var hashChanged = function(){
	  oHash = document.location.hash;
	  var w = oHash.match(/iphone/i)?0
			:(oHash.match(/blackberry/i)?1
			:(oHash.match(/android/i)?2
			:(oHash.match(/symbian/i)?3
			:(oHash.match(/windows/i)?4
			:(oHash.match(/pc/i)?5
			:'')))));
	  $('.tabButton:eq('+w+')').click();
  };
  checkHash();
  
});

/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);


/* My Account and Sign Up Link Selectors */
$(document).ready(function(){
    var mL = $('.myAccountLink');
    if(mL.length && mL.text().length){
    	$("a:contains('My Account')").attr('href',mL.text());
    }
    var lL = $('.logoLink');
    if(lL .length && lL .text().length){
    	$("a img[src*=logo_myglobal.gif]").parent().attr('href',lL.text());
    }
    var fL = $('.faqLink');
    if(fL .length && fL .text().length){
    	$("a:contains('FAQ')").attr('href',fL.text());
    }
    var sL = $('.signUpLink');
    if(sL.length && sL.text().length){
    	signUpURL = sL.text();
    }
});