function getSectionId(str) {
    var myregexp = /sec=(\d{1,4})/;
    var match = myregexp.exec(str);
    
    if (match != null) {
        result = match[1];
    } else {
        result = "";
    }

    return result;
}

var originalFontSize;

function handleFontSize() {
	//	Set original font size
	if (!($.cookie("OriginalFontSize"))) {
		originalFontSize = $("html").css("font-size");
		$.cookie("OriginalFontSize", originalFontSize, { path: '/' });
	}
	else {
		originalFontSize = $.cookie("OriginalFontSize");	
	}
	
	//	Set font size if user change it in a last 30 days
	if ($.cookie("FontSize")) {
		$("html").css("font-size", $.cookie("FontSize"));	
	}
	
	//	Reset Font Size
	$("#font_original").click(function(event){
		$("html").css("font-size", originalFontSize);
		$.cookie("FontSize", null, { path: '/' });
		if (window.frames[0]) window.frames[0].changeFontSize1(1) ;
		event.preventDefault();
	});
	
	//	Increase Font Size
	$("#font_large").click(function(event){
		changeFontSize(1.12);
		if (window.frames[0]) window.frames[0].changeFontSize1(1.12) ;
		event.preventDefault();
	});
	
	//	Increase more Font Size
	$("#font_larger").click(function(event){
		changeFontSize(1.20);
		if (window.frames[0]) window.frames[0].changeFontSize1(1.20) ;
		event.preventDefault();
	});
}

function changeFontSize(fontMultiplier) {
	//	var currentFontSize = $("html").css("font-size");
		
	var currentFontSize =  $.cookie("OriginalFontSize");
	
	var currentFontSizeNum = parseFloat(currentFontSize, 10);
	var newFontSize = currentFontSizeNum * fontMultiplier;
	
	$.cookie("FontSize", newFontSize, { path: '/', expires: 30 });
	
	$("html").css("font-size", newFontSize);
}

function handleNavigation() {
	//	Mark selected root section
	$(".level1 > li > a[title='" + topSectionName + "']").addClass("selected").parent().find("ul:first").show();
	
	//	Mark selected section
	//	$(".level2 a[title='" + currentSectionName + "']").addClass("selected");
	$(".level2 a").each(function(){
		var $link = $(this);
		var linkSectionId = getSectionId($link.attr("href"));
		
		if (linkSectionId != "") {
			if (currentSectionId == linkSectionId) {
				$link.addClass("selected");	
			}	
		}
		else {
			if ($link.attr("title") == currentSectionName) {
				$link.addClass("selected");	
			}	
		}
	});
	
	//	Mark header link
	$("a.LinkText_Links span").each(function() {
		if ($.trim($(this).text()) == currentSectionName) {
			$(this).parent().removeClass("LinkText_Links").addClass("LinkText_Links_selected");
		}								   
	});
}

function handlePageTheme() {
	var heightContrastTheme = false;
	
	//	Set theme on document load
	if ($.cookie("HeightContrastPageTheme")) {	
		heightContrastTheme = ($.cookie("HeightContrastPageTheme") == "false") ? false : true;
		
		if (heightContrastTheme) {
			changeTheme(true);	
		}
	}
	
	//	Set theme on button click
	$("#contrast").click(function(event){
		event.preventDefault();
		
		heightContrastTheme = !heightContrastTheme;
		
		$.cookie("HeightContrastPageTheme", heightContrastTheme, { path: '/', expires: 30 });
		
		changeTheme(heightContrastTheme);
	});
	
	function changeTheme(heightContrast) {
		var oStyles = $("link[title='styles']");
		var oStylesContrast = $("link[title='stylescontrast']");
		
		var strRel = oStyles.attr("rel");
		var strRelContrast = oStylesContrast.attr("rel");
		
		oStyles.attr("rel", strRelContrast).attr("disabled", heightContrast);
		oStylesContrast.attr("rel", strRel).attr("disabled", !heightContrast);
	}
}

function handlePageFont() {
	var sansSerifFont = false;
	
	//	Set font on document load
	if ($.cookie("SansSerifPageFont")) {	
		sansSerifFont = ($.cookie("SansSerifPageFont") == "false") ? false : true;
		
		if (sansSerifFont) {
			changeFont(true);	
		}
	}
	
	//	Set font on button click
	$("#font_type").click(function(event){
		sansSerifFont = !sansSerifFont;
		
		$.cookie("SansSerifPageFont", sansSerifFont, { path: '/', expires: 30 });
		
		changeFont(sansSerifFont);
	});
	
	function changeFont(sansSerif) {
		var oStylesSansSerif = $("link[title='stylessansserif']");
		
		var strRel = oStylesSansSerif.attr("rel");
		
		if (!sansSerif) {
			strRel = "alternate stylesheet";
		}
		else {
			strRel = "stylesheet";
		}
		
		oStylesSansSerif.attr("rel", strRel).attr("disabled", sansSerif);
	}
}

function NavTo(url){
	window.location = url;
}

function pageInitialize() {
	//	Handle page themes
	handlePageTheme();
	
	// Handle page font
	handlePageFont();
	
	//	Handle font size
	handleFontSize();
	
	//	Handle navigation treee
	handleNavigation();
	
	//	Handle tabs
	$(".RecentAndLinksContainer, .FotoAndVideoContainer").pmTabs();
	
	//	Function for smooth scrolling
	$('#top-link').click(function(e) {
		e.preventDefault();
		$.scrollTo(0,300);
	});
	
	//	Handle article tabs
	$(".ArticleWrapper_TabLine").pmArticleTab({
		contentContainer: ".Lead_Text",
		tabContainer: ".ArticleTabWrapper",
		tabHeadingLevel: "h3",
		selectedTabClass: "selected"
	});
	
	$(".ArticleTabWrapper").each(function(){
		$(this).find("li:first").addClass("first");
		$(this).find("li:last").addClass("last");
	});
	
	// Apply LighBox gallery
	$(".lightbox").each(function(e){
		var oImg = $(this).children("img");
		var strImageSrc = oImg.attr("src").substring(0, oImg.attr("src").length - 3) + "800";
			
		$(this).attr("href", strImageSrc).attr("title", oImg.attr("alt"));
	});
	
	$(".lightbox").lightBox({
		imageLoading: "DesignTemplates/DORH/images/_gui/lightbox-ico-loading.gif",
		imageBtnClose: "DesignTemplates/DORH/images/_gui/lightbox-btn-close.gif",
		imageBtnPrev: "DesignTemplates/DORH/images/_gui/lightbox-btn-prev.gif",
		imageBtnNext: "DesignTemplates/DORH/images/_gui/lightbox-btn-next.gif",
		txtImage: labelGalleryImage,
		txtOf: labelGalleryImageOf																													 
	});
	
	//	Handle article video
	$(".ArticleLinksWrapper img").each(function(){
		var oImg = $(this);
		var oVideoLink = oImg.parents("a:first");
		var oVideoContainer = oImg.parents("div:first"); 
		
		oVideoContainer.attr("id", "videoPlayer");
		
		var s1 = new SWFObject('designtemplates/dorh/include/_media/player.swf','ply','450','300','9','#ffffff');
		
		s1.addParam('allowfullscreen','true');
		s1.addParam('allowscriptaccess','always');
		s1.addParam('wmode','opaque');
		s1.addParam('flashvars','type=video&file=' + oVideoLink.attr("href") + '&image=' + oImg.attr("src"));
		
		s1.write('videoPlayer');
	});
}