﻿var rightPanel_wpHeaderTD_class = "rightWPHeaderTD";

var imagesPath = "/Sehetna-Layout/images/";
var printCssPath = "/Sehetna-Layout/print.css";
var blankImgUrl = imagesPath + "spacer.gif";
var firstChild_Class = "firstChild";

var defaultPageURL = "/Pages/default.aspx";

var fontSize = 0;

addthis_config = {
    ui_language: "ar",
    ui_cobrand: "صحتنا"
}


$(document).ready(function() {

    $(".leftPart h3.ms-WPTitle span:even").each(function(i) {
        $(this).html('<div>' + $(this).html() + '</div>');
    });

    // other code here
    $(".rightPart h3.ms-WPTitle, .leftPart .tblSplited h3.ms-WPTitle").each(function(i) {

        var strOldText = $(this).text();

        $(this).html('<div><div><img src="' + blankImgUrl + '" alt="" width="1" height="29" style="width:1px; height:29px;" />' + $(this).text() + '</div></div>');


        var strNewText = $(this).text();



        ////////////document.write(str.replace(/Microsoft/g, "W3Schools"));

        $(this).parent().addClass(rightPanel_wpHeaderTD_class);

        // === Fixing RightPart - WebParts Titles =============
        /*
        $("div.rightPart tr.ms-WPHeader td, div.rightPart tr.ms-WPHeader td img, div.rightPart tr.ms-WPHeader td a" 
        + ", .tblSplited tr.ms-WPHeader td a, .tblSplited tr.ms-WPHeader td img").each(function (i) {
		
			if( $(this).attr("title") != undefined){
        $(this).attr( "title", $(this).attr("title").replace(strOldText , strNewText ) );
        }
			
			if( $(this).attr("alt") != undefined){
        $(this).attr( "alt", $(this).attr("alt").replace(strOldText , strNewText ) );			
        }
        });
        */

        $(".rightWPHeaderTD").each(function(i) {
            if ($(this).attr("title") != undefined) {
                $(this).attr("title", "");
            }
        });




    });

    if ($.browser.msie && $.browser.version == "6.0") {

        //alert("Im the annoying IE6");

        // Do PngFix ============

        $('.rightPart h3.ms-WPTitle div div, .tblSplited h3.ms-WPTitle div div').pngFix();

    }

    $(".ms-ToolPaneTitle span").each(function(i) {
        $(this).html($(this).text());
        $(this).html($(this).text());
    });



    /* ======{ Making Splitted Main WebPart Zones the same Height }====== */
    var boolHasContent = true;

    $(".tblSplited > tbody > tr > td").each(function(i) {
        if ($.trim($(this).text()) == "") {
            $(this).hide();
            boolHasContent = false;
        }

        if (!boolHasContent) {
            $(this).css("width", "100%");
            $(this).parent().parent().parent().attr("cellspacing", "0");
        }

    });

    if (boolHasContent) {
        var maxHeight = 0;
        $(".tblSplited .cCenter").each(function(i) {
            maxHeight = Math.max(maxHeight, $(this).height());
        });
        $(".tblSplited .cCenter").css("height", maxHeight + "px");
    }

    /* ======{ Checking and Hide Main WebPart Zones if they are Empty }====== */

    $(".contentBox, .tblSplited").each(function(i) {
        if ($.trim($(this).text()) == "") {
            $(this).hide();
        }
    });

    /* ======{ After Doing Fixes .. Display the PageContent }====== */

    $("div.pageContentMain, div.contentBox").css({
        "visibility": "visible"
    });

    // IE 6 Fix For Country Selection Buttons
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {

        $('.countryList').css({
            "visibility": "hidden",
            "display": "block"
        });


        $('.countryList').css({
            "visibility": "visible",
            "display": "none"
        });
    }



    $('.btnCountry').click(function() {
        //$(this).slideUp();
        $('.countryList').toggle("medium");
    });

    $('.countryList a').click(function() {
        $('.countryList').toggle("medium");
    });


    /* ======{ WebPart TaskPane Drag & Drop support }====== */
    $('.ms-ToolPaneOuter').css("height", "").append('<tr><td><div class="wpResizeHandle"></div></td></tr>'); // ").

    $("#MSOTlPn_Tbl").jqDrag("#MSOTlPn_ToolPaneCaption").jqResize(".wpResizeHandle");
});


// ==================================================


$(document).ready(function() {

    /* ======{ TopNav / LifeStages Item Selection }====== */
    var lnkGlobal = 'http://www.sehetna.com/pages/default.aspx';
    var lnkEgypt = 'http://www.sehetna.com.eg/';
    var lnkJordan = 'http://www.sehetna.com.jo/';

    $(".flagGlobal").attr("href", lnkGlobal);
    $(".flagEg").attr("href", lnkEgypt);
    $(".flagJo").attr("href", lnkJordan);

});


/* ======{ SideBar Menu }====== */

var treeOpened_class = "treeOpened";

$(document).ready(function() {
    /*
    // Adding Title ( for MouseOver tooltip )
    $(".sideBarMenu h3").attr("title", $(".sideBarMenu h3").text() );
    */

    var menuRoot = $(".sideBarMenu div > ul");

    $("ul", menuRoot).each(function() {
        $(this).css("display", "none");
    });

    $("li", menuRoot).prepend('<img src="' + blankImgUrl + '" alt="" width="12" height="12" />');

    $("li img, li a[href='#']", menuRoot).click(function() {
        //$("ul:first", $(this).parent() ).slideToggle("fast").parent().toggleClass( treeOpened_class );
        toggleTreeMenu($("ul:first", $(this).parent()));
    });

    // (20091209) Open the Selected Item Parent Tree
    toggleTreeMenu($("ul:first", $(".sideBarMenu div ul > li > ul > li.SubCatSelected").parent().parent()));

    /*
    // Example to Open First SubMenu
    toggleTreeMenu( $("ul:first", menuRoot) );
    */
});


function toggleTreeMenu(ulSubMenu) {

    $(ulSubMenu).slideToggle("fast").parent().toggleClass(treeOpened_class);

}

/* ======{ SubCat Menu }====== */

function ToggleSelectedSubCatMenu(thisObj) {
    $(document).ready(function() {

        toggleTreeMenu($("ul:first", $(thisObj).parent()));

    });
}

/* ======{ Site Index Menu }====== */
var strSiteIndexURL = "/Pages/az.aspx?l=";

$(document).ready(function() {
    var intLnkID = 1;

    $(".TopIndexLinks a").each(function() {
        //$("ul:first", $(this).parent() ).slideToggle("fast").parent().toggleClass( treeOpened_class );

        //toggleTreeMenu( $("ul:first", $(this).parent() ) );

        $(this).attr("href", strSiteIndexURL + intLnkID++);
    });

    /*	
    $("table.tblGrid td:first-child, table.tblGrid th:first-child").each(function() {
    $(this).addClass( firstChild_Class );
    $(this).css("background", "#ff0000");
    });
    */

});


$(document).ready(function() {
    //$(".breadCrumb").jBreadCrumb({easing:'swing'});

    //$(".breadCrumb").jBreadCrumb({easing:'swing', beginingElementsToLeaveOpen:0});

    $(".breadCrumb li:first-child").addClass('first');

    var breadCrumbLastItem = $(".breadCrumb li:last-child");
    breadCrumbLastItem.addClass('last');
    // Disable the Last Item Link...
    if ($(".breadCrumb li").length != 1) {
        breadCrumbLastItem.html(breadCrumbLastItem.text());
    }

    $(".breadCrumb li").append('<span class="breadCrumbArrow">&nbsp;&gt;&nbsp;</span>');

});


// ==================================================

var topNavSelected_Class = "AspNet-Menu-Selected";

var topNavItemText = "المراحل الحياتية";

var topNavItemText = "المراحل الحياتية";

$(document).ready(function() {

    /* ======{ TopNav / LifeStages Item Selection }====== */


    var strSelector = ".topNav > div > ul > li";


    $(strSelector + " > a, ").each(function(i) {
        if ($.trim($(this).text()) == $.trim(topNavItemText)) {
            $(this).attr("href", "#");
        }
    });


    //$.queryString()['ls']
    var strUrl = location.href.toLowerCase();

    var lifeStageID = $.queryString()['ls'] + "";

    var menuItemID = "#tnItem";

    if (isUnderLifeStage == true) {
        if (lifeStageID == "7") {
            menuItemID += "2";
        } else {
            menuItemID += "1";
        }
    } else {
        //if in  /Lists/Forums/ , /Wiki/ , /PodCast/ , /Blog/

        if ((strUrl.indexOf("/lists/forums/") != -1) | (strUrl.indexOf("/wiki/") != -1) | (strUrl.indexOf("/podcast/") != -1) | (strUrl.indexOf("/blog/") != -1)) {
	         menuItemID += "3";
        }else if(strUrl.indexOf("/doclib1/") != -1){


        //if in /DocLib1/  select tnItem4
		menuItemID += "4";
	}



    }

    // Select the active TopNavigation Tab
    $(menuItemID).addClass(topNavSelected_Class);

    /* WORKING =======================
    if( lifeStageID == "7" ){
    //$.trim( $(this)
    topNavItemText = $( strSelector + ":eq(2) > a").text();
    }
	
	if(isUnderLifeStage == true){
    $( strSelector ).removeClass( topNavSelected_Class );

		$(strSelector  + " > a, ").each(function (i) {
    if ( $.trim( $(this).text() ) == $.trim( topNavItemText ) ){
    $(this).attr("href","#");
    $(this).parent().addClass( topNavSelected_Class );
    }
    });
    }
    */

    var lastItem = $(strSelector + ":last-child");

    if (!(lastItem.hasClass(topNavSelected_Class))) {
        lastItem.addClass('lastItem');
    }

    $(".topNav > div > ul").css({
        "visibility": "visible"
    });



    // Fixing Page Title ( Removing Title Image of MOSS ) 
    $(".contentTitle h3").html($(".contentTitle h3").text());



    if ((strUrl.match(defaultPageURL.toLowerCase()) != null) && (isUnderLifeStage == false)) {
        $(strSelector + ":first-child").addClass(topNavSelected_Class);
    }

    /*
    var lifeStagesLink = '/LifeStages/';
    var strUrl = location.href.toLowerCase();

	if ( strUrl.match( lifeStagesLink.toLowerCase() ) != null){
    $(strSelector  + " > a").each(function (i) {
			
    strUrl = $(this).attr("href").toLowerCase();

			if ( strUrl.match( lifeStagesLink.toLowerCase()) != null){
    $(this).parent().addClass( topNavSelected_Class );
    }
    });
    }
    */
});


$(document).ready(function() {

    // Fixing SharePoint List Views Width Issue
    if ($(".contentHolderDiv .ms-menutoolbar:first-child").width() > $(".contentHolderDiv").width()) {
        $(".divExtraCssCode").html('<style type="text/css">.contentHolderDiv{overflow:scroll;}</style>');
    }

    fontSize = parseInt($(".contentHolderDiv").css("font-size"));

    //var fontChange = 1;

    $(".fontUp").click(function() {
        var fontSizeNew = parseInt($(".contentHolderDiv").css("font-size"));
        changeFontSize(fontSizeNew + 2);
    });

    $(".fontReset").click(function() {
        changeFontSize(fontSize);
    });


    $(".fontDown").click(function() {
        var fontSizeNew = parseInt($(".contentHolderDiv").css("font-size"));
        changeFontSize(fontSizeNew - 1);
    });
    // Wiki Edit Panel Bar fix
    $('div.ms-wikieditouter').parent().attr("style","");
});


function changeFontSize(newSize) { 

	// [Start] Added 20100329 --[ Text Pasted From MS Word Fix ]---
		if( !isConvertedToEm ) fontSizer_ConverteToEm();
	// [ END ] Added 20100329 -------------------------------------
	
    if (newSize > 22) newSize = 22;
    if (newSize < 9) newSize = 9;
    //$(".contentHolderDiv").css("font-size", newSize + "px" );
    $(".contentHolderDiv").animate({
        fontSize: newSize + "px"
    }, "fast");

}


function _doPrint() {
    var div = $(".leftPart");
    var win = window.open("", "win");
    var frame = "<html><head><link type='text/css' href='" + printCssPath + "' rel='stylesheet' /></head>";
    frame += "<body><table dir='rtl' width='100%' cellpadding='10'><tr><td align='center'>";
    frame += "<img src='/site images/logo.gif'></td></tr><tr><td align='center'>";
    frame += "<hr size='1' width='100%'/></td></tr>";
    frame += "<tr><td height='70%'><table border=0>";
    frame += div.innerHTML;
    frame += "</table></td></tr><tr><td><hr size='1' width='100%'/></td></tr>";
    frame += "<tr><td height='10%' align='center'><button id='btnPrint' onclick='btnClose.click();window.print();'>طباعة</button>&nbsp;<button style='display:none' id='btnClose' onclick='close();'>إغلاق</button><button id='btnCloseWindow' onclick='window.close();'>إغلاق</button>";
    frame += "</td></tr></table></body></html>";
    win.document.write(frame);
}





// ====== QueryString Support ================================

//alert( $.queryString()['ls'] );
jQuery.queryString = function() {
    var r = {};
    var q = location.search;
    q = q.replace(/^\?/, ''); // remove the leading ?        
    q = q.replace(/\&$/, ''); // remove the trailing &
    jQuery.each(q.split('&'), function() {
        var key = this.split('=')[0];
        var val = this.split('=')[1];
        // convert floats
        if (/^[0-9.]+$/.test(val))
            val = parseFloat(val);
        // ingnore empty values
        if (val)
            r[key] = val;
    });
    return r;

}; 


// [START] Added 20100329 =========================================
var isConvertedToEm = false;
function fontSizer_ConverteToEm(){
	
	///=={Note by Monir}=========/
	/// Using [this.style.fontSize] instead of using [$(this).css("font-size")] because:
	/// jQuery CSS Automatically returns size on PX, and we Need to check if it's EM
	///================/
	
	//var strOutput = "";
	var intCounter = 0;
	var strTempFontSize = "";

	$(".contentHolderDiv span").each(function (i) {
		if( $(this).attr("style") != undefined ){
			strTempFontSize = this.style.fontSize;
			
			// Checking if the font-size is already in em or not
			if( strTempFontSize.search(/em/i) == -1){
				// not EM based font-size, so convert it
				
				/// Here I'm Using $(this).css("font-size") because
				/// I need the size value to be in Pixels (PX) to Calucate Em Value
				
				strTempFontSize = parseFloat( $(this).css("font-size") ) / fontSize;
				strTempFontSize += "em";
				
				this.style.fontSize = strTempFontSize;
				//strOutput += strTempFontSize + " | "; //$(this).css("font-size") + " | ";
			}
			/*
			else{
				// is already EM based font-size
				//////alert( $(this).text() );
			}
			*/
			   
			intCounter++;
		}
	});
	//alert(intCounter + "\n-------\n\n" + strOutput);
	isConvertedToEm = true;
}
// [ END ] Added 20100329 =========================================
