// Common scripts
$(document).ready(function(){
						   
	var lang = getLang()[0];
	var acr = getLang()[1];
	
	// Set css lang
	secure = document.location.href.indexOf("https");
	domain = "";
	if ( secure != -1 ) domain = "https://www2.brastel.com";
	var cssLang = getLang()[0];
	$("#langCss").attr("href", domain + "/GenericFiles/4.0/css/styles_"+ cssLang + ".css")
	$("#fontsCss").attr("href", domain + "/GenericFiles/4.0/css/fonts/fonts_"+ cssLang + ".css")
	
	var links = document.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++) { 
   links[i].onfocus=function(){this.blur()}
	}
	
	// for Home page featureList
	$('#tabs li a').featureList({
		output				:	'#output li',
		start_item			:	0,
		transition_interval	: 	15000
	});
	
	$('.hdg2Link').click(function(){
		var $h2Contents = $(this).next("div.hdg2ContentsHidden");
		var $target = $(this);
		var $target2 = $("h2:first");
		if ($h2Contents.is(":visible")){
			$h2Contents.animate({opacity:0},500).slideUp(500,function(){
				if ( $target2.is(":above-the-top") ){
					$.scrollTo($target2,700);}
			}).animate({opacity:1},500);}
		else
			{
				if ($("div.hdg2ContentsHidden:visible").length)
					{	
						var $visibleDiv = $("div.hdg2ContentsHidden:visible");
						var $indexOfClicked = $("h2.hdg2Link").index(this);
						var $prevH2 = $("div.hdg2ContentsHidden:visible").prevAll(".hdg2Link:first");
						var $indexOfVisible = $("h2.hdg2Link").index($prevH2) ;
						var $visibleDivHeight = $visibleDiv.height();
						var $h2ContentsHeight = $h2Contents.height();
						
						if ( $indexOfClicked > $indexOfVisible )
							{
								var $nextTitles = $visibleDiv.nextAll("h2.hdg2Link");
								
								if ($visibleDivHeight>$h2ContentsHeight)
									{
										var $difference = $visibleDivHeight - $h2ContentsHeight;
										$("div#adjuster").height($difference).animate({opacity:1},500).show(1);
									}
								
								$nextTitles.animate({opacity:0},500);
								$(this).animate({opacity:0},500).animate({opacity:1},500);
								$visibleDiv.animate({opacity:0},500).hide(1).animate({opacity:1},500);
								$h2Contents.animate({opacity:0},500).show(1);
								$("h2.hdg2Link:first").fadeTo(1000,1,function(){
									$.scrollTo($target,500);
									$("h2.hdg2Link:first").fadeTo(500,1,function(){
										$h2Contents.animate({opacity:1},500);
										$("h2.hdg2Link").animate({opacity:1},500);
										$("div#adjuster").hide(1000);
									});
								});
							}
						else
							{
								$h2Contents.fadeIn(500);
								$visibleDiv.hide();
							}							
					}
				else
					{
						$h2Contents.fadeIn(500);
					}
			}
	});
	
	/*
	$('.spcTitleLogoHolder').gradient({
			from:      'FBF8C8',
			to:        'F8EE4F',
			direction: 'vertical'
	});	
	$('.generalNotesTitle p').gradient({
			from:      'FFFFFF',
			to:        'E6F3FF',
			direction: 'horizontal'
	});
	*/
	
	setIfrSources();
	
	// close special holidays calendar
	$("div.specialHolidaysP img.close").live("click",function() {
		$("div.specialHolidaysP").slideUp();
	});
	// close customer service language menu
	$("div#csvLangMenu img.close").live("click",function() {
		$("div#csvLangMenu").slideUp();
	});
	
	// change customer service language
	$("div#csvLangMenu ul li a").live("click",function() {
		$("div#csvLangMenu").slideUp();
		var freeDialSpan = "div#" + $(this).attr("class") + " span.freedial";
		var freeDialNum = $(freeDialSpan).html();
		var notfreeDialSpan = "div#" + $(this).attr("class") + " span.notfreedial";
		var notfreeDialNum = $(notfreeDialSpan).html();
		var faxSpan = "div#" + $(this).attr("class") + " span.fax";
		var faxNum = $(faxSpan).html();
		var emailSpan = "div#" + $(this).attr("class") + " span.email";
		var emailAddr = $(emailSpan).html().replace("punto",".");
		var nameSpan = "div#" + $(this).attr("class") + " span.name";
		var langName = $(nameSpan).html();

		$titleDiv = $("iframe#ifrFooter").contents().find("div.title");
		$freedialSpan = $("iframe#ifrFooter").contents().find("span#freedial");
		$notfreeSpan = $("iframe#ifrFooter").contents().find("span#notfreedial");
		$faxSpan = $("iframe#ifrFooter").contents().find("span#fax");
		$tables = $("iframe#ifrFooter").contents().find("div.csvInfoHolder");
		$langLink = $("iframe#ifrFooter").contents().find("div.langMenuLink");

		$titleDiv.animate({opacity:0},500);
		$tables.animate({opacity:0},500);
		$langLink.animate({opacity:0},500);
		
		
		$(this).oneTime(500, function () {
			$("iframe#ifrFooter").contents().find("span#freedial").html(freeDialNum);
			$("iframe#ifrFooter").contents().find("span#notfreedial").html(notfreeDialNum);
			$("iframe#ifrFooter").contents().find("span#fax").html(faxNum);
			$("iframe#ifrFooter").contents().find("span#email").html(emailAddr);
			$("iframe#ifrFooter").contents().find("span#name").html(langName);
			$("iframe#ifrFooter").contents().find("span#emailLink").attr({href:"mailto:" + emailAddr});
		});
		$(this).oneTime(500, function () {
			$titleDiv.animate({opacity:1},500);
			$tables.animate({opacity:1},500);
			$langLink.animate({opacity:1},500);
		});
	});
	
	// show brastel location menu
	
	var hashval = SWFAddress.getValue().replace(/\/*(.*)\/*/,"$1");
	var selectedLocation = $.cookie("brastel_selected_loc");
	if ( hashval == "location" && !selectedLocation ) {
		$("body").prepend('<div id="selCountrySpacer"></div><div id="selectCountry"><div class="wrapper"><img src="/GenericFiles/4.0/img/country_sel/logo.png" alt="Brastel Telecom" class="logo" /></div></div>');
		$("div#selectCountry div.wrapper").append('<img class="text" src="/GenericFiles/4.0/img/country_sel/select_location.png" alt="Select your location" />');
		$("div#selectCountry div.wrapper").append('<div class="buttons"></div>');

		var buttons = '<div id="selCountryHelpBtn" class="btnHelp"></div>' +
		'<div id="selectUSA" class="button"></div>' +
		'<div id="selectBrazil" class="button"></div>' +
		'<div id="selectJapan" class="button"></div>';
		$("div#selectCountry div.wrapper div.buttons").append(buttons);
		
		$("body").append('<div id="opaqueBg"></div>');
		$("div#opaqueBg").animate({opacity:0});
		var bodyHeight = $("body").outerHeight();
		var selCountryHeight = $("div#selectCountry").height();
		
		$(document).oneTime(6000,function(){
			$("div#selectCountry").slideDown();
			$("div#selCountrySpacer").slideDown();
			$("div#header_bg").slideUp();
			$("div#opaqueBg").oneTime(700,function(){
			$(this).show().css({"top":selCountryHeight+1,"height":bodyHeight-selCountryHeight}).animate({opacity:0.7},500);
			});
		});
		
		var helpHtml = 	'<div id="selCountryHelp">'+
							'<div class="wrapper">'+
								'<img class="arrowUp" src="/GenericFiles/4.0/img/country_sel/arrow_up.png" alt="">'+
								'<img class="close" src="/GenericFiles/4.0/img/common/close.png" alt="" />'+
								'<div class="contents">'+
									'<p class="text">Select the country where your Brastel Card was issued.<br />'+
									'If you do not have a Brastel Card yet, select the country where you reside.</p>'+
									'<div class="langMen">'+
										'<div class="menu selected eng">English<span>Select the country where your Brastel Card was issued.<br />If you do not have a Brastel Card yet, select the country where you reside.</span></div>'+
										'<div class="menu por">Português<span>Selecione o país onde seu Cartão Brastel foi emitido.<br />Se você ainda não possui um Cartão Brastel, selecione o país onde você mora.</span></div>'+
										'<div class="menu jpn">日本語<span>現在お住まいの国またはブラステルカードの発行国をお選びください。</span></div>'+
										'<div class="menu ind">Indonesia<span>Pilih negara dimana Kartu Brastel anda diterbitkan.<br />Jika anda belum memiliki Kartu Brastel, pilih negara dimana anda bertempat tinggal.</span></div>'+
										'<div class="menu esp">Español<span>Seleccione el país donde su Brastel Card fue emitida. <br />i aún no tiene una Brastel Card, seleccione el país en donde reside.</span></div>'+
										/*'<div class="menu chi">中國語<span></span></div>'+*/
										'<div class="menu rus">Русский<span>Выберите страну, в которой Вы приобрели карточку Брастел. Если у Вас еще нет карточки Брастел, выберите страну проживания.</span></div>'+
										'<div class="menu tha">ไทย<span>เลือกประเทศที่คุณทำการออกบัตรบรัสเทล<br />หากคุณยังไม่มีบัตรบรัสเทล กรุณาเลือกประเทศที่คุณอาศัยอยู่</span></div>'+
										'<div class="menu kor">한국어<span>브라스텔 카드가 발행된 국가를 선택해 주십시오.<br />브라스텔 카드가 없는 고객은 거주하고 계신 국가를 선택해 주십시오.</span></div>'+
										'<div class="menu tag">Tagalog<span>Piliin ang bansa kung saan ang inyong Brastel Card ay na-issue.<br />Kung kayo ay wala pang Brastel Card, piliin lamang ang bansa kung saan kayo ay naninirahan.</span></div>'+
										/*'<div class="menu vie">Tiếng Việt<span></span></div>'+*/
										/*'<div class="menu fre">Français<span></span></div>'+*/
										/*'<div class="menu rom">Română<span></span></div>'+*/
										/*'<div class="menu tur">Türkçe<span></span></div>'+*/
									'</div>'+
								'</div><br style="clear:both" />'+
							'</div>'+
						'</div>';
		$("body").append(helpHtml);
		$("div#selCountryHelp").css({"top":selCountryHeight-8});
	}
	else if ( hashval == "location" && selectedLocation == "br" ) {
		$.cookie("brastel_selected_loc","br", {expires: 365, path: '/'});
		window.location.href = ("http://www.brastel.com.br/")
	}
	else if ( hashval == "location" && selectedLocation == "us" ) {
		$.cookie("brastel_selected_loc","us", {expires: 365, path: '/'});
		window.location.href = ("http://www.brastel.net/")
	}
	else {
		$.cookie("brastel_selected_loc","jp", {expires: 365, path: '/'});
	}
	
	$("div#selectJapan").click(function(){
		hideCountrySel();
		_gaq.push(['_trackEvent', 'Country selection', 'from slide-down menu', 'Japan']);
		$.cookie("brastel_selected_loc","jp", {expires: 365, path: '/'});
	});
	$("div#selectBrazil").click(function(){
		_gaq.push(['_trackEvent', 'Country selection', 'from slide-down menu', 'Brazil']);
		$.cookie("brastel_selected_loc","br", {expires: 365, path: '/'});
		window.location.href = ("http://www.brastel.com.br/")
	});
	$("div#selectUSA").click(function(){
			_gaq.push(['_trackEvent', 'Country selection', 'from slide-down menu', 'U\.S\.A\.']);
			$.cookie("brastel_selected_loc","us", {expires: 365, path: '/'});
			window.location.href = ("http://www.brastel.net/")
	});
	
	$("div#opaqueBg").click(function(){
		hideCountrySel();
		$.cookie("brastel_selected_loc","jp", {expires: 365, path: '/'});
	});
	
	$("div#selCountryHelpBtn").click(function(){
		$("div#selCountryHelp").fadeIn();
	});
	$("#selCountryHelp .menu").mouseover(function(){
		var txt = $(this).find("span").html();
		$("#selCountryHelp p.text").animate({opacity:0},150).oneTime(150,function(){
			$(this).html(txt).animate({opacity:1},1)
		});
		$("#selCountryHelp .menu.selected").toggleClass("selected");
		$(this).toggleClass("selected");
	});
	$("#selCountryHelp img.close").click(function(){
		$("div#selCountryHelp").hide();
	});
	
	//Set event gallery iframe src
	var eventGalleryIfrSrc = "http://" + domain + "/web/WIMS/Manager.aspx?xslFile=event_gallery.xsl&xmlPreview=event_gallery.xml&Style=1&Service=1&acr=" + acr;
	$("iframe#eventGalleryIfr").attr("src",eventGalleryIfrSrc)
	
	// Embed FLIP campaign flash
	var swfFile = "/GenericFiles/4.0/swf/flip_campaign/flip_" + lang + ".swf";
	var flipFlashParams = {
		wmode:"transparent"
	}
	swfobject.embedSWF(swfFile, "flipCampaignFlash", "739", "509", "9.0.0",false,false,flipFlashParams,false,false)
						   
$(".roundedcorners1").corner({
	tl: { radius: 1 },
	tr: { radius: 1 },
	bl: { radius: 1 },
	br: { radius: 1 },
	antiAlias: true,
	autoPad: true
});
$(".roundedcorners2").corner({
	tl: { radius: 2 },
	tr: { radius: 2 },
	bl: { radius: 2 },
	br: { radius: 2 },
	antiAlias: true,
	autoPad: true
});
$(".roundedcorners3").corner({
	tl: { radius: 3 },
	tr: { radius: 3 },
	bl: { radius: 3 },
	br: { radius: 3 },
	antiAlias: true,
	autoPad: true
});
$(".roundedcorners4").corner({
	tl: { radius: 4 },
	tr: { radius: 4 },
	bl: { radius: 4 },
	br: { radius: 4 },
	antiAlias: true,
	autoPad: true
});
$(".roundedcorners5").corner({
	tl: { radius: 5 },
	tr: { radius: 5 },
	bl: { radius: 5 },
	br: { radius: 5 },
	antiAlias: true,
	autoPad: true
});
$(".roundedcorners6").corner({
	tl: { radius: 6 },
	tr: { radius: 6 },
	bl: { radius: 6 },
	br: { radius: 6 },
	antiAlias: true,
	autoPad: true
});
$(".roundedcorners7").corner({
	tl: { radius: 7 },
	tr: { radius: 7 },
	bl: { radius: 7 },
	br: { radius: 7 },
	antiAlias: true,
	autoPad: true
});
$(".roundedcorners8").corner({
	tl: { radius: 8 },
	tr: { radius: 8 },
	bl: { radius: 8 },
	br: { radius: 8 },
	antiAlias: true,
	autoPad: true
});
$(".roundedcorners9").corner({
	tl: { radius: 9 },
	tr: { radius: 9 },
	bl: { radius: 9 },
	br: { radius: 9 },
	antiAlias: true,
	autoPad: true
});
$(".roundedcorners10").corner({
	tl: { radius: 10 },
	tr: { radius: 10 },
	bl: { radius: 10 },
	br: { radius: 10 },
	antiAlias: true,
	autoPad: true
});

}); //-- $(document).ready end

function hideCountrySel(){
	$("div#selCountryHelp").hide();
	$("div#opaqueBg").animate({opacity:0},500).hide()
	$("div#selectCountry").slideUp();
	$("div#selCountrySpacer").slideUp();
	$("div#header_bg").slideDown();
}

function setIfrSources()
{
	var lang = getLang()[0];
	var acr = getLang()[1];
	var langVerySign = getLang()[2];
	var page = getPage()[0];
	var pagegrp = getPage()[1];
	var category = getPage()[2];
	
	var langParam = "lang=" + lang;
	var acrParam = "acr=" + acr;
	var langVerySignParam = "lang=" + langVerySign;
	var pageParam = "page=" + page;
	var pagegrpParam = "pagegrp=" + pagegrp;
	var categoryParam = "category=" + category;
	
	var header = document.getElementById("ifrHeader");
	var mainMenu = document.getElementById("ifrMainMenu");
	var footer = document.getElementById("ifrFooter");
	var footer_privacy = document.getElementById("ifrFooterPrivacy");
	var inPreviewMode = previewMode();
	var server = "";
	
	function previewMode() {
		if (getParentParamNames()[0] == "qsPageCD") return true; else return false;
	}
	
	if (inPreviewMode) var server = "http://192.168.1.2";
	if (secure != -1) var server = "https://www2.brastel.com";
	
	if (header)
	{
		$("iframe#ifrHeader").attr({src: server + "/web/WIMS/Manager.aspx?xslFile=header.xsl&xmlPreview=languages.xml&Style=1&Service=1&" + acrParam + "&" + pageParam + "&" + categoryParam});
	}
	if (mainMenu)
	{
		$("iframe#ifrMainMenu").attr({src: server + "/" + lang + "/menu/" + category + "/?" + pageParam + "&" + pagegrpParam});
	}
	if (footer)
	{
		$("iframe#ifrFooter").attr({src: server + "/web/WIMS/Manager.aspx?xslFile=footer.xsl&xmlPreview=languages.xml&Style=1&Service=1&" + acrParam});
	}
	if (footer_privacy)
	{
		$("iframe#ifrFooterPrivacy").attr({src: server + "/web/WIMS/Manager.aspx?xslFile=privacyfooter.xsl&xmlPreview=languages.xml&Style=1&Service=1&" + acrParam + "&" + pageParam + "&" + categoryParam});
	}
}

var newwindow = "";
function popUp(url,name,specs) {
	window.name='openerWin';
	if (!newwindow.closed && newwindow.location)
		{
			newwindow.location.href = url;
		}
	else
		{
			newwindow=window.open(url,name, specs);
			if (!newwindow.opener) { newwindow.opener = self; }
		}
	if (window.focus) 
		{
			newwindow.focus()
		}
	return true;
}

function runtest() {
	alert(getPage());
}

function alertcookie() {
	alert(readCookie("spcAddSvcMenuOpn"))
}
function alertPage() {
	alert(getPage())
}
