var $j = jQuery.noConflict(); 
 $j(document).ready(function () {
	/* HOMEPAGE  BEGIN */
	$j('.tnLocale > a').click(function (e)
	{
		e.preventDefault();
		$j('#localeSelector').toggle();
	});
	$j('.sendText').click(function(){
window.open( "http://www.inmarsat.com/Services/Land/IsatPhone/SMS/sms.html", "myWindow", 
"status = 1, height = 500, width = 375, resizable = 0" )
});
	initialise('#scroller-holder'); 
	/* HOMEPAGE  END */
	initialiseProduct('.opener'); // PRODUCT PAGE
	retailerinit();
	
	bindYoutube();//your say
  });
/* PRODUCT PAGE  BEGIN */
function initialiseProduct(parrent){
	$j(parrent).each(function(){
		var current = this;
		$j(current).children('div').height(0);
		var tempdimension = $j(current).children('div').children('ul').size()>0?$j(current).children('div').children('ul').height()+parseInt($j(current).children('div').children('ul').css('padding-top'))+$j($j(current).children('div').children('div')[0]).height():$j(current).children('div').children('div').height();
		
		var is_tabs = $j(current).children('div').children('ul').size()>0?1:0;
		$j(this).children('h2').click(function(e){
			var str = '';
			$j(current).children('div').children('div').children('OL').children('LI').each(function(){ str += ' ' + $j(this).height(); });
			//alert(str);
			//alert($j(current).children('div').children('div').children('OL').children('LI')[0].style);
			$j(current).children('h2').children('a').toggleClass('open');
			if($j(current).children('div').hasClass('is_open') == true){
				$j(current).children('div').animate({ height: 0}, 1000);
				$j(current).children('div').removeClass('is_open');
			}else{
				if(is_tabs){
					$j(current).children('div').children('ul').find('li > .active').removeClass('active');
					$j($j(current).children('div').children('ul').children('li')[0]).children('a').addClass('active');
					$j(current).children('div').children('div').addClass('jspCap');
					$j($j(current).children('div').children('div')[0]).removeClass('jspCap');

				}
				$j(current).children('div').addClass('is_open');
				$j(current).children('div').animate({ height: tempdimension}, 1000);
			}
			e.preventDefault();
		});
		if(is_tabs){
			$j(current).children('div').children('ul').children('li').each(function(){
				var currentbutton = this;				
				$j(this).children('a').click(function(e){
					e.preventDefault();
					$j(current).children('div').children('ul').find('li > .active').removeClass('active');
					$j($j(current).children('div').children('ul').children('li')[$j(currentbutton).index()]).children('a').addClass('active');
					$j(current).children('div').children('div').addClass('jspCap');
					$j($j(current).children('div').children('div')[$j(currentbutton).index()]).removeClass('jspCap');
					$j(current).children('div').height($j(current).children('div').children('ul').height()+parseInt($j(current).children('div').children('ul').css('padding-top'))+$j($j(current).children('div').children('div')[$j(currentbutton).index()]).height());
					
				});
			});
		}
	});
                                $j('#product-review-table td.value').click(function() {
                                                $j(this).parent().children().removeClass('starred');
//                                                $j(this).children('input').attr('checked', 'checked'); // not cross browser, firefox
							var radio = $j(this).children('input')[0];
                					radio.checked = !radio.checked;
                                                ind = $j(this).index();
                                                for(i=0;i<ind;i++) {
                                                                $j(this).parent().children('td').eq(i).addClass('starred');
                                                }
                                });
$j('.open-review').click(function(){
	$j('.open-review').parent().parent().children('ol').children('li').css('display','block');
	$j('.open-review').parent().parent().parent().height($j('.open-review').parent().parent().height());
});

	$j('.video-link').each(function(){$j(this).overlay({
					onClose: function() {
						$j("object").each(function(){
							id = $j(this).attr("id");
							try
							{
								getFlashMovie(id).stopVideo();
							}
							catch(err){}
							//stopVideo($id);
						});
						//player.unload();
				}});});
}
/* PRODUCT PAGE  END */

/* HOMEPAGE  BEGIN */
var currentlocation = 1;
function getFlashMovie(movieName) {
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function initialise(parrent) {
	var newul = document.createElement('ul');
	newul.setAttribute('class','nav-pag');
	newul.setAttribute('className', 'nav-pag');
	$j(parrent + ' > #scroller').width(960).height(317).children('a').each(function(){
			var newdiv = document.createElement('div');
			newdiv.setAttribute('class','slide floatLeft');
			newdiv.setAttribute('className', 'slide floatLeft');
			$j(newdiv).html($j(this).clone());
			$j(newdiv).css('display','none');
			if($j(newdiv).children('a').children('img').attr('alt') != "")
			{
				var relation = '#'+ $j(newdiv).children('a').children('img').attr('alt');
				$j(newdiv).attr('rel',relation);
				$j(newdiv).overlay({
					onClose: function() {
						$j("object").each(function(){
							id = $j(this).attr("id");
							try
							{
								getFlashMovie(id).stopVideo();
							}
							catch(err){}
							//stopVideo($id);
						});
						//player.unload();
				}});
			}
			$j(parrent + ' >  #scroller').append($j(newdiv));
			$j(this).remove();
			$j(newul).append('<li style="display:inline;"><a href="#"></a></li>');
			
		});
	$j(parrent + ' >  #scroller > div::nth-child(1)').css('display','block');
	$j(parrent + ' #nav-pag-left').before($j(newul));
	$j(parrent + ' > #scroller-nav').width($j(parrent + ' .nav-pag').width()+86);
	$j(parrent + ' > #scroller-nav > #slider-nav-bg > div').width($j(parrent + ' .nav-pag').width());
	$j(parrent + ' > #scroller-nav > .nav-pag > li::nth-child(1)').children('a').addClass('sel');
	$j(parrent + ' > #scroller-nav > .nav-pag').children().each(function() { 
		var current = this;
		$j(this).click( function(e) {
			$j(parrent + ' >  #scroller > div::nth-child('+currentlocation+')').fadeOut(500,function(){
				
				currentlocation = $j(current).index()+1;
//$j('.scroll-pane').jScrollPane();
			});
			$j(parrent + ' >  #scroller > div::nth-child('+($j(current).index()+1)+')').fadeIn(500,function(){
				$j(parrent + ' >  #scroller > div::nth-child('+($j(current).index()+1)+')').css('display','block');}
				);
			$j(parrent + ' > #scroller-nav > .nav-pag').children().each(function() { 
				$j(this).children('a').removeClass('sel');
			});
			$j(this).children('a').addClass('sel');
			e.preventDefault();
		});
	});
	setTimeout("changeScroller('"+parrent+"',"+(currentlocation+1)+")",6000);

}
function changeScroller(parrent,target) {
	if(currentlocation>=($j(parrent + ' > #scroller-nav > .nav-pag').children().size())){
		var current = $j(parrent + ' > #scroller-nav > .nav-pag').children('::nth-child('+1+')');
	}
	else
	{
		var current = $j(parrent + ' > #scroller-nav > .nav-pag').children('::nth-child('+(currentlocation+1)+')');
	}
	$j(parrent + ' >  #scroller > div::nth-child('+currentlocation+')').fadeOut(500,function(){
		currentlocation = $j(current).index()+1;
	});
	$j(parrent + ' >  #scroller > div::nth-child('+($j(current).index()+1)+')').fadeIn(500,function(){
		$j(parrent + ' >  #scroller > div::nth-child('+($j(current).index()+1)+')').css('display','block');}
	);
	$j(parrent + ' > #scroller-nav > .nav-pag').children().each(function() { 
		$j(this).children('a').removeClass('sel');
	});
	$j(current).children('a').addClass('sel');
	setTimeout("changeScroller('"+parrent+"',"+(currentlocation+1)+")",6000);
}
/* HOMEPAGE  END  */


/*FIND A RETAILER BEGIN */

var distributor,partner;
function retailer(){
	if($j('#retailers-main').size()>0){
		$j.ajax({
		  type: "GET",
		  url: 'countryList.xml',
		  success: function(data) {
			var counter = 1;
			$j(data).children('data').children().each(function () {
				$j('#retailers-main select').append('<option value="' + $j(this).children('name').text()+'|'+$j(this).children('region').text() + '">' + $j(this).children('name').text() + '</option>');
				counter++;
			});
			$j('#retailers-main select').change(function(){
				if($j("select option:selected").index() !=0){
				changecountry($j("select option:selected").val());
				$j('#local-distributor').css('display','block');
				}
			});
		  }
		});
		$j.ajax({
		  type: "GET",
		  url: 'distributionPartner.xml',
		  success: function(data) {
			var counter = 1;
			distributor = data;
		  }
		});
		$j.ajax({
		  type: "GET",
		  url: 'isatPhoneData.xml',
		  success: function(data) {
			var counter = 1;
			partner = data;
		  }
		});
	
	}
}
function addretailers(thiselement) {
	$j('#local-retailers').css('display','block');
		var $cloneretailers = $j('.clones').children('.element').clone();
		$cloneretailers.children('h4').html($j(thiselement).children('companyname').text());
		$cloneretailers.children('.address-panel').children('.location').html($j(thiselement).children('city').text()+', '+$j(thiselement).children('country').text());
		if($j(thiselement).children('mainphone').text() != '0'){
			$cloneretailers.children('.address-panel').children('.tel').children('.value').html($j(thiselement).children('mainphone').text());
		}else{
			$cloneretailers.children('.address-panel').children('.tel').css('display','none');
		}
		if($j(thiselement).children('mainemail').text() != '0'){
			$cloneretailers.children('.address-panel').children('.email').children('.value').html($j(thiselement).children('mainemail').text());
		}else{
			$cloneretailers.children('.address-panel').children('.email').css('display','none');
		}
		if($j(thiselement).children('mainurl').text() != '0'){
			$cloneretailers.children('.address-panel').children('.web').children('.value').html($j(thiselement).children('mainurl').text());
		}else{
			$cloneretailers.children('.address-panel').children('.web').css('display','none');
		}
		$cloneretailers.find('.email-link').attr("href", "mailto:"+$j(thiselement).children('mainemail').text());
		$cloneretailers.find('.email-link').after('<div class="services '+$j(thiselement).children('rank').text().toLowerCase()+'"></div>');
		$j('#local-retailers').children('div').append($cloneretailers);
}
function changecountry(newcountry){
	var tempvariable = newcountry;
	newcountry = newcountry.split('|')[0];
	newregion = tempvariable.split('|')[1];
	$j('#local-retailers').children('div').html('');
	$j('#local-retailers').css('display','none');
	$j(partner).children('data').find("country name:contains('"+newcountry+"')").parent().children('companies').children().each(function(){
		addretailers(this);

	});
	$j('#local-retailers').children('div').append('<div><h1>Regional distributors</h1></div>');
	$j(partner).children('data').find("region name:contains('"+newregion+"')").parent().children('country').each(function(){
		if($j(this).children('name').text() != newcountry) {
		$j(this).children('companies').children().each(function(){
		addretailers(this);

		});}
	});
	
	
}

/*FIND A RETAILER END */

/*YOUR SAY BEGIN */
function bindYoutube(){
	$j('#yoursay-main #youtube li a').click(function(e) {
		url = 'http://www.youtube.com/embed/' + $j(this).attr('rel');
		$j('#youtube iframe').attr('src',url);
		e.preventDefault();
	});

}
/*YOUR SAY END */


function retailerinit() {
	
if($j('#retailers-main').size()>0){
		
		$j.getScript("http://www.google.com/recaptcha/api/js/recaptcha_ajax.js");
		$j.ajax({
		  type: "GET",
		  url: 'custom/home/country',
		  success: function(data) {
			var counter = 1;
			$j(data).children('data').children().each(function () {
				$j('#retailers-main select').append('<option value="' + $j(this).children('name').text()+/*'|'+$j(this).children('region').text() +*/ '">' + $j(this).children('name').text() + '</option>');
				counter++;
			});
			$j('#retailers-main select').change(function(){
				if($j("select option:selected").index() !=0){
				show_retailers_loading();
				changecountry_new($j("select option:selected").val());
				}
			});
		  }
		});
}
}


function show_retailers_loading() {
	$j('#retailers-main').prepend('<div class="loading-overlay"><img src="/skin/frontend/base/default/images/loading-overlay.gif" alt="" /></div>');
}
function hide_retailers_loading() {
	$j('#retailers-main .loading-overlay').remove();
	$j('#retailers-main h3').show();
	$j('.inpage-nav').show();
}


function changecountry_new(param){
	if($j('#retailers-main').size()>0){
		$j.ajax({
			  type: "GET",
			  url: 'custom/home/index/'+param,
			  success: function(data) {
				hide_retailers_loading();
				$j('.import').show();
				$j('.import').html(data);
				$j('.import .close-form').click(function(e){
					e.preventDefault();
					$j('.partner-help:visible').each(function(){
						$j(this).hide();
						$j(this).parent().find('.nav a.selected i').remove();
						$j(this).parent().find('.nav a.selected').removeClass('selected');
					});
					$j('#recapcha-image').remove();
				});
				$j('.scroll-top').click(function(e){
					e.preventDefault();
					window.scroll(0,0);
				});
				var clone = $j('.import #recapcha-image').clone();
				$j('.import').find('.partner-box').each(function(){
					var currentparrent = this;
					
					$j(this).find('.disabled').click(function(e){e.preventDefault(); return false;})
					
					$j(this).find('.img-partner').each(function(){
						try{_gaq.push(['_trackEvent', 'Qualified_Lead', $(this).attr("src"), $j($j(currentparrent).find('.partner-details > h3')[0]).html()]);}catch(err){}
					});

					$j(this).find('.phone-form-click').not('.disabled').each(function(){
						$j(this).click(function(e){
							e.preventDefault();
							$j($j('.partner-help:visible')[0]).parent().find('.nav a > i').remove();
							$j($j('.partner-help:visible')[0]).parent().find('.nav a').removeClass('selected');
							$j($j('.partner-help:visible')[0]).hide();
							if($j(this).parent().parent().hasClass('nav')){
								$j(this).append('<i><img src="/skin/frontend/base/default/images/partner-help-top.png" alt="" /></i>');
								$j(this).addClass('selected');
							} 
							$j('#recapcha-image').remove();
							$j($j(currentparrent).find('.partner-help > form.phone-form')[0]).parent().parent().children('.partner-help').hide()
							$j($j(currentparrent).find('.partner-help > form.phone-form')[0]).parent().toggle(true);
							$j($j(currentparrent).find('.partner-help > form.phone-form  div.recapcha-field')[0]).append(clone);
							regenerateimage();
						});
					});
					$j(this).find('.email-form-click').not('.disabled').each(function(){
						$j(this).click(function(e){
							e.preventDefault();
							$j($j('.partner-help:visible')[0]).parent().find('.nav a > i').remove();
							$j($j('.partner-help:visible')[0]).parent().find('.nav a').removeClass('selected');
							$j($j('.partner-help:visible')[0]).hide();
							if($j(this).parent().parent().hasClass('nav')){
								$j(this).append('<i><img src="/skin/frontend/base/default/images/partner-help-top.png" alt="" /></i>');
								$j(this).addClass('selected');
							} else {
								$j(this).parent().parent().parent().parent().parent().find('ul.nav a.email-form-click').append('<i><img src="/skin/frontend/base/default/images/partner-help-top.png" alt="" /></i>').addClass('selected');
							}
							$j('#recapcha-image').remove();
							$j($j(currentparrent).find('.partner-help > form.email-form')[0]).parent().parent().children('.partner-help').hide();
							$j($j(currentparrent).find('.partner-help > form.email-form')[0]).parent().toggle(true);
							$j($j(currentparrent).find('.partner-help > form.email-form  div.recapcha-field')[0]).append(clone);
							regenerateimage();
						});
					});
	
					$j($j(currentparrent).find('.partner-help > form.email-form')[0]).submit(function() {
						  var validation = verifyform('email',this);
						  if(validation != true){
							  $j($j(currentparrent).find('.partner-help > form.email-form div.errors')[0]).html(validation);
						  } else {
							  $j($j(currentparrent).find('.partner-help > form.email-form div.errors')[0]).html('');
								$j.ajax({
								  type: "POST",
								  url: 'custom/home/email_form',
								  data: "sendtoname="+$j($j(this).find('input[name="sendtoname"]')[0]).val()+"&company="+$j($j(this).find('input[name="company"]')[0]).val()+"&sendto="+$j($j(this).find('input[name="sendto"]')[0]).val()+"&name="+$j($j(this).find('input[name="name"]')[0]).val()+"&email="+$j($j(this).find('input[name="email"]')[0]).val()+"&phone="+$j($j(this).find('input[name="phone"]')[0]).val()+"&assist="+$j($j(this).find('select[name="assist"]')[0]).val()+"&comment="+$j($j(this).find('textarea[name="comment"]')[0]).val()+"&subscribe="+($j(this).find('input[name="subscribe"]')[0].checked?1:0)+"&recaptcha_response_field="+$j($j(this).find('input[name="recaptcha_response_field"]')[0]).val()+"&recaptcha_challenge_field="+Recaptcha.get_challenge(),
								  success: function(data) {
									  if(data == 'success')
										  {
										  	$j($j(currentparrent).find('.success_form')[0]).show();
										  	try{_gaq.push(['_trackEvent', 'Qualified_Lead', 'send email', $j($j(currentparrent).find('.partner-details > h3')[0]).html()]);}catch(err){}
										  	$j('.partner-help:visible').hide();
											$j('#recapcha-image').remove();
											$j($j(currentparrent).find('.success_form')[0]).show();
										  } else{
											  regenerateimage();
											  $j($j(currentparrent).find('.partner-help > form.email-form div.errors')[0]).html(data);
										  }
								  }
								});
							  
						  }
						  return false;
					});
					$j($j(currentparrent).find('.partner-help > form.phone-form')[0]).submit(function() {
						  var validation = verifyform('phone',this);
						  if(validation != true){
							  $j($j(currentparrent).find('.partner-help > form.phone-form div.errors')[0]).html(validation);
						  } else {
							  $j($j(currentparrent).find('.partner-help > form.phone-form div.errors')[0]).html('');
								$j.ajax({
								  type: "POST",
								  url: 'custom/home/telephone_form',
								  data: "sendtoname="+$j($j(this).find('input[name="sendtoname"]')[0]).val()+"&company="+$j($j(this).find('input[name="company"]')[0]).val()+"&sendto="+$j($j(this).find('input[name="sendto"]')[0]).val()+"&name="+$j($j(this).find('input[name="name"]')[0]).val()+"&email="+$j($j(this).find('input[name="email"]')[0]).val()+"&phone="+$j($j(this).find('input[name="phone"]')[0]).val()+"&assist="+$j($j(this).find('select[name="assist"]')[0]).val()+"&comment="+$j($j(this).find('textarea[name="comment"]')[0]).val()+"&subscribe="+($j(this).find('input[name="subscribe"]')[0].checked?1:0)+"&recaptcha_response_field="+$j($j(this).find('input[name="recaptcha_response_field"]')[0]).val()+"&recaptcha_challenge_field="+Recaptcha.get_challenge(),
								  success: function(data) {
									  if(data == 'success')
										  {
										  	$j($j(currentparrent).find('.success_form')[0]).show();
										  	try{_gaq.push(['_trackEvent', 'Qualified_Lead', 'call me', $j($j(currentparrent).find('.partner-details > h3')[0]).html()]);}catch(err){}
										  	$j('.partner-help:visible').hide();
											$j('#recapcha-image').remove();
											$j($j(currentparrent).find('.success_form')[0]).show();
										  } else{
											  regenerateimage();
											  $j($j(currentparrent).find('.partner-help > form.phone-form div.errors')[0]).html(data);
										  }
								  }
								});
							  
						  }
						  return false;
					});
				});
				
			  }
			});
	}
}

function verifyform(type,parrent){
	var error = '';
	if($j($j(parrent).find('input[name="name"]')[0]).val() == '') {
		error = 'Name is a required field';
		return error;
	}
	if($j($j(parrent).find('input[name="email"]')[0]).val() == '') {
		error = 'Email is a required field';
		return error;
	}
	if(type == 'phone') {
		if($j($j(parrent).find('input[name="phone"]')[0]).val() == '') {
			error = 'Phone is a required field';
			return error;
		}
	}
	return true;
}
function regenerateimage(){
	//Public key
	Recaptcha.create("6Le4HsUSAAAAAFDN3dU0I_2C-qZ8y3PnPZrMZNfn",
		    "recapcha-image",
		    {
		      theme: "white"
		      //callback: Recaptcha.focus_response_field
		    }
		  );
}
