﻿var subsite = document.location.href;
var relURL = '';
  	
function SelectSubsiteGroup(language)
{	
	var index = subsite.toLowerCase().indexOf('/'+language+'/');

  	if(index > 0)
  	{
  		relURL = subsite.substring(index);
  		subsite = subsite.substring(index + language.length + 2);
  		index = subsite.indexOf('/');
  		if(index > 0)
  		{
  			subsite = subsite.substring(0, index).toLowerCase();
  			if(subsite != 'pages' && subsite != 'list')
  			{
  				$('a.'+subsite+'Group').addClass(subsite+'GroupSelected');
  			}
  		}
  	}
}

function HideSubmenuGroups()
{
	//Hiding all subgroup divs
	$('div.ChildsGroupDiv').css('display', 'none');
	var SubGroup = $('#'+subsite+'SubItems');
	if(SubGroup.length > 0)
		SubGroup.css('display', 'block');
}

function SelectCurrentMenuItem()
{
	if(relURL.length > 10)
	{
		var currLink = $('a[href*="'+relURL+'"]');
		if(currLink.length > 0)
		{
			if(currLink.hasClass('NavSubItem'))
			{//Then sub item, select it.
				currLink.addClass('NavSubItemSelected');
			}
			else if(currLink.hasClass('NavSubItem2'))
			{//Then sub accordion item, select it, select its parent and expand its accordion
				currLink.addClass('NavSubItemSelected2');
				var parentGroup = currLink.parent().parent().prev('div').children();
				
				if(parentGroup.length > 0)
					parentGroup.addClass('NavSubItemSelected');
			}
		}
	}
}

function ShowTab(tabID)
{
    $('.TabContent').addClass('Hidden');
    $('.TabHead').removeClass('TabHeadSelected');
    $('#'+tabID).addClass('TabHeadSelected');
    $('#tc_'+tabID).removeClass('Hidden');
}

function ShowTab2(tabID)
{
    $('.TabPicContent').addClass('Hidden');
    $('.TabHeadPic').removeClass('th_1ClassActive').removeClass('th_2ClassActive').removeClass('th_3ClassActive').removeClass('th_4ClassActive').removeClass('th_5ClassActive').removeClass('th_6ClassActive');
    $('#'+tabID).addClass(tabID+'ClassActive');
    $('#tc_'+tabID).removeClass('Hidden');
}

//Overriding default Function, which is called when survey is submit, to show thanking popup.
function PreSaveAction()
{
	if($('td.mainheader:contains("Respond to this Survey")').length > 0 || 
		$('td.mainheader:contains("الاستجابة لهذا الاستطلاع")').length > 0)
	{//Then this is a survey
		var langIndex = subsite.toLowerCase().indexOf('/arabic');
		if(langIndex > 0)
			alert('شكرا لك على المشاركة بإستطلاع الرأى');
		else
			alert('Thank you for participating in the survey');
	}
	return true;
}

//Function to append to breadcrumb
function AppendToBreadCrumb(title, url)
{
	var breadcrumb = $('span[id*="PlaceHolderTitleBreadcrumb_siteMapPath"]')
	if(breadcrumb.children().length > 4)
	{
		breadcrumb.append('<span>&nbsp;>&nbsp;</span>');
		breadcrumb.append('<span><a title="'+title+'" class="ms-sitemapdirectional" href="'+url+'">'+title+'</a></span>');
	}
}

/***********************************************/
function GoToDefaultSite()
{
	//document.location. = '/arabic';
	location.replace('/arabic');//Works with FF & IE
}
/***********************************************/
//Main Groups Accordion Content section
var ContentHeight = 0;
var TimeToSlide = 250.0;
var openAccordion = '';

function runAccordion(index) 
{
	var nID = "Accordion" + index + "Content";
	if (openAccordion == nID)
		nID = '';
	$('.selected').removeClass('selected');
	$('#Accordion'+index+'Header').addClass('selected');
	ContentHeight = $('#Accordion'+index+'Content').height();
	if(ContentHeight <=0)
		ContentHeight = 200;
		
	setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ",'"+ openAccordion + "','" + nID + "')", 33);
	openAccordion = nID;
}

function animate(lastTick, timeLeft, closingId, openingId) 
{
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;

	var opening = (openingId == '') ? null : document.getElementById(openingId);
	var closing = (closingId == '') ? null : document.getElementById(closingId);

	if (timeLeft <= elapsedTicks) {
		if (opening != null)
			opening.style.height = ContentHeight + 'px';

		if (closing != null) {
			closing.style.display = 'none';
			closing.style.height = '0px';
		}
		return;
	}

	timeLeft -= elapsedTicks;
	var newClosedHeight = Math.round((timeLeft / TimeToSlide) * ContentHeight);

	if (opening != null) {
		if (opening.style.display != 'block')
			opening.style.display = 'block';
		opening.style.height = (ContentHeight - newClosedHeight) + 'px';
	}

	if (closing != null)
		closing.style.height = newClosedHeight + 'px';

	setTimeout("animate(" + curTick + "," + timeLeft + ",'"+ closingId + "','" + openingId + "')", 33);
}

function RemoveHostNameFromURL()
{
	if($('#contentToPrint').length > 0)
	{
		/*var replacedHTML = $('#contentToPrint').html();
		replacedHTML = replacedHTML.replace("http://mbthq-extspapp1:5050", "");
		replacedHTML = replacedHTML.replace("http://mbthq-extspfe01:5050", "");
		replacedHTML = replacedHTML.replace("http://mbthq-extspfe02:5050", "");
		$('#contentToPrint').html(replacedHTML);
		*/
		//alert('Test' + $('#contentToPrint a[href*="http://mbthq-extspapp1:5050"').length);
		$('a[href^="http://mbthq-extspapp1:5050"]').each(function(index) 
		{
			var val = $(this).attr('href').replace("http://mbthq-extspapp1:5050", "");
			$(this).attr('href', val);
		});
		
		$('img[src^="http://mbthq-extspapp1:5050"]').each(function(index) 
		{
			var val = $(this).attr('src').replace("http://mbthq-extspapp1:5050", "");
			$(this).attr('src', val);
		});
	}
	
	//for Homepage Only
	if($('.topbgcurvestrip').length > 0)
	{
		$('a[href^="http://mbthq-extspapp1:5050"]').each(function(index) 
		{
			var val = $(this).attr('href').replace("http://mbthq-extspapp1:5050", "");
			$(this).attr('href', val);
		});
	}
}

function getURLParam(strParamName)
{
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 ){
    var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if ( 
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}
/***********************************************/

$(document).ready(function()
{
	//Hide first two elements of the breadcrumb ("Home >")
	var breadcrumb = $('span[id*="PlaceHolderTitleBreadcrumb_siteMapPath"]')
	if(breadcrumb.children().length > 2)
	{
		//For Discussions board breadcrumb
		if(breadcrumb.children().length > 4)
		{
			if(breadcrumb.children().eq(4).html() == "Discussion Boards")
				breadcrumb.children().eq(4).html('<a class="ms-sitemapdirectional" href="/English/DiscussionBoards">Discussion Boards</a>');
			else if(breadcrumb.children().eq(4).html() == "منتديات الحوار")
				breadcrumb.children().eq(4).html('<a class="ms-sitemapdirectional" href="/Arabic/DiscussionBoards">منتديات الحوار</a>');
		} 
		breadcrumb.children().eq(0).css('display','none');
		breadcrumb.children().eq(1).css('display','none');
	}
	
	//For Qatar at Glance page
	//JSRequest.EnsureSetup(); 
	var qs = getURLParam('tabID');//JSRequest.QueryString['tabID'];
	if(qs != null && qs != '' && qs <= 6)
	{
		ShowTab2('th_'+qs);
	}
	
	//Running accordion, selecting first tab by default.
	if($('#AccordionContainer').length > 0)
	{
		runAccordion(1);
	}
		
	//For fixing CEWP link Full URL issue
	RemoveHostNameFromURL();
});
