
/************** **************/

function setDate() {
    var d_names = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

    var m_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

    var d = new Date();
    var curr_day = d.getDay();
    var curr_date = d.getDate();
    var curr_month = d.getMonth();
    var curr_year = d.getFullYear();
    $('.dateset').text(d_names[curr_day] + ' ' + curr_date + ' ' + m_names[curr_month] + ' ' + curr_year);

}

var searchCookieName = 'savedCCSearch';

var bookmarkCookieName = 'savedCCBookmark';

$(document).ready(function () {
    setDate();
    $(".addPageBookmark").click(function (event) {
        event.preventDefault();
        var currentTime = new Date();
        var month = currentTime.getMonth() + 1;
        var day = currentTime.getDate();
        var year = currentTime.getFullYear();
        var date = month + "/" + day + "/" + year;
        var val = $.cookie(bookmarkCookieName);
        var newVal = location.pathname + location.search + "~" + date;
        if (val === null || val.indexOf(newVal) < 0) {
        	// START :: CHG1041762 :: Change to Saved Bookmarks to sort them in order of being saved, latest being the first.
        	val = val === null ? newVal : newVal + "||" + val;
        	// END :: CHG1041762 :: Change to Saved Bookmarks to sort them in order of being saved, latest being the first.
            $.cookie(bookmarkCookieName, val, { path: '/', expires: 2000 });
            var val = $.cookie(bookmarkCookieName);
            if (val === null) {
                alert("Bookmarks require cookies to be enabled")
            }
            else {
                updateBookmarks($("#viewBookMark"), val);
            }
        }

    });
    $(".addSearchBookmark").click(function (event) {
        event.preventDefault();
        var currentTime = new Date();
        var month = currentTime.getMonth() + 1;
        var day = currentTime.getDate();
        var year = currentTime.getFullYear();
        var date = month + "/" + day + "/" + year;
        var totalResults = document.getElementById('TResults');
        var val = $.cookie(searchCookieName);
        var newVal = '';
        //Added for blank search result Start
        if(totalResults === null || totalResults.value === ''){
        	newVal = location.pathname + location.search + "~" + date + "~" + 0;
        }else{
        	newVal = location.pathname + location.search + "~" + date + "~" + totalResults.value;
        }
        //Added for blank search result End
        if (val === null || val.indexOf(newVal) < 0) {
        	// START :: CHG1039482 :: Change to Saved Searches to sort them in order of being saved, latest being the first.
        	val = val === null ? newVal : newVal + "||" + val;
        	// END :: CHG1039482 :: Change to Saved Searches to sort them in order of being saved, latest being the first.
            $.cookie(searchCookieName, val, { path: '/', expires: 2000 });
            var val = $.cookie(searchCookieName);
            if (val === null) {
                alert("Bookmarks require cookies to be enabled")
            }
            else {
                updateBookmarks($("#viewSavedSearch"), val);
            }
        }

    });
    updateBookmarks($("#viewBookMark"), $.cookie(bookmarkCookieName));
    updateBookmarks($("#viewSavedSearch"), $.cookie(searchCookieName));

    $("#headersearchformQuery").blur(function (event) {
        if ($(this).val() == '') {
            $(this).val('Enter keywords');
        }
    });
    $("#headersearchformQuery").focus(function (event) {
        if ($(this).val() == 'Enter keywords') {
            $(this).val('');
        }
    });
    $("#headersearchformPubs, #headersearchformNews, #headersearchformPeople").change(function () {
        if ($(this).is(":checked")) {
            $("#headersearchformAll").attr('checked', false);
        }
    });
    $("#headersearchformClose").click(function (event) {
        event.preventDefault();
        $(this).closest('.hover').removeClass('hover');
    });
    $("#headersearchformSubmit").click(function () {
        if ($("#headersearchformQuery").val() == 'Enter keywords' || $("#headersearchformQuery").val() == '') {
            return false;
        }
    });
});

function updateBookmarks(element, val) {

    if (val === null || val === 0) {
        $(element).html('');
    }
    else {
        $(element).html('(' + val.split("||").length + ')');
    }
}





/****************************** END USER TOOLBAR/BOOKMARKING JAVASCRIPT *************************/

var popupItem;

/****************************** VIEW MAP AND CONTACT PERSON POPUP WINDOWS ************************/
function contactCliffordChance(url) {
    if ($("#contactCCForm").size() === 0) {
        $("body").append('<div id="contactCCForm"><div style="display:block;"><a style="float:right" href="#" class="closePopup" title="Close Popup"><img src="/etc/designs/newcc/images/crossBtnBlack.gif" alt="Close Popup Window" /></a><div style="clear:both"></div></div><div id="contactCCHTML"></div></div>');
        $("#contactCCForm .closePopup").click(function (event) {
            event.preventDefault();
            disablePopup($("#contactCCForm"));

        });
        if ($("#backgroundPopup").size() === 0) {
            $("body").append('<div id="backgroundPopup" title="Click here to close popup"></div>');
            $("#backgroundPopup").click(function (event) {
                event.preventDefault();
                disablePopup($("#contactCCForm"));
            });

        }
    }
    $.ajax({
        type: "GET",
        url: url,
        async: true,
        success: function (data) {
            $("#contactCCHTML").html(data);
            centerPopup($("#contactCCForm"));
            $("#backgroundPopup").css({ "opacity": "0.7" });
            $("#backgroundPopup").fadeIn("slow");
            $("#contactCCForm").fadeIn("slow");
            //Let's put the focus back on the popUp
            $("#contactCCForm .closePopup").focus();

        }
    });

}

function viewCliffordChanceOfficeMap(url, alt) {
    if ($("#cliffordChanceOfficeMap").size() === 0) {
        $("body").append('<div id="cliffordChanceOfficeMap"><div style="display:block;"><a style="float:right" href="#" class="closePopup" title="Close Popup"><img src="/etc/designs/newcc/images/crossBtnBlack.gif" alt="Close Popup Window" /></a><div style="clear:both"></div></div><img id="viewOfficeMap" src="" alt="" /></div>');
        $("#cliffordChanceOfficeMap .closePopup").click(function (event) {
            event.preventDefault();
            disablePopup($("#cliffordChanceOfficeMap"));
        });

        if ($("#backgroundPopup").size() === 0) {
            $("body").append('<div id="backgroundPopup" title="Click here to close popup"></div>');
            $("#backgroundPopup").click(function () {
                disablePopup($("#cliffordChanceOfficeMap"));
            });
        }
    }
    $("#viewOfficeMap").attr("src", url);
    $("#viewOfficeMap").attr("alt", alt);
    centerPopup($("#cliffordChanceOfficeMap"));
    $("#backgroundPopup").css({ "opacity": "0.7" });
    $("#backgroundPopup").fadeIn("slow");
    $("#cliffordChanceOfficeMap").fadeIn("slow");
    //Let's put the focus back on the popUp
    $("#cliffordChanceOfficeMap .closePopup").focus();

}

$(document).ready(function () {
    $(".contactPerson").click(function (event) {
        event.preventDefault();
        popupItem = $(this);
        contactCliffordChance($(this).attr("href"));



    });
    $(".officeMap").click(function (event) {
        event.preventDefault();
        popupItem = $(this);
        viewCliffordChanceOfficeMap($(this).attr("href"), $(this).attr("title"));

    });
});

function disablePopup(ele) {
    $("#backgroundPopup").fadeOut("slow");
    ele.fadeOut("slow");
    $(popupItem).focus();
}


//centering popup
function centerPopup(ele) {
    //request data for centering
    var scrolledX = document.body.scrollLeft || document.documentElement.scrollLeft || self.pageXOffset || 0;
    var scrolledY = document.body.scrollTop || document.documentElement.scrollTop || self.pageYOffset || 0;

    var screenWidth = document.body.clientWidth || document.documentElement.clientWidth || self.innerWidth;
    var screenHeight = document.documentElement.clientHeight;


    var left = scrolledX + (screenWidth - ele.width()) / 2;
    if (left < 0) {
        left = 0;
    }
    var top = scrolledY + (screenHeight - ele.height()) / 2;
    if (top < 0) {
        top = 0;
    }

    $(ele).css({ "position": "absolute", "top": top, "left": left });
    //only need force for IE6
    $("#backgroundPopup").css({ "height": (document.body.clientHeight || document.documentElement.clientHeight || self.innerHeight) });
}

/****************************** END VIEW MAP AND CONTACT PERSON POPUP WINDOWS ************************/


/****************************** HOMEPAGE CAROUSEL ****************************************************/
function carrouselStop(time) {
    if (interval) {
        clearInterval(interval);
        timeout = setTimeout(carrouselStart, time);
    }
    interval = null;

}
function carrouselStart(time) {
    $(".pagingRight").click();
    clearTimeout(timeout);
    interval = setInterval(carrouselStart, time);


}
function updatePagination(curr, max) {
    $("#pageNumbering").text(curr + " / " + max);
}





/********************** Tabs ***********************/
$.fn.simpleTabs = function () {
    //Default Action
    //On Click Event

    //$(this).find("ul.tabbed li .tabheader:first").click(); //Show first tab content    
}; //end function

$(document).ready(function () {
    $("ul.tabbed li .tabheader").click(function (event) {
        event.preventDefault();
        var simpleTabs = $(this).closest(".simpleTabs");
        $(this).closest("ul.tabbed").find("li.activeTab").removeClass("activeTab"); //Remove any "active" class
        $(this).parent().addClass("activeTab"); //Add "active" class to selected tab
        simpleTabs.find(".tabContent").hide(); //Hide all tab content
        var activeTab = $(this).parent().find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
        var classTab = activeTab.replace("#", ".");
        simpleTabs.find(classTab).show(); //Fade in the active content      

        simpleTabs.find(classTab + ' a:first').focus();

        simpleTabs.find(classTab + " .simpleTabs .activeTab .tabheader").click();
        var hreflang = $(this).parent().find("a").attr('hreflang');
        if (hreflang !== undefined) {
            hreflang = '&amp;cc_language=' + hreflang;
        }
        else {
            hreflang = '';
        }
        classTab = '&amp;cc_tab=' + classTab.replace(".", "");
        sitestat(cliffordChanceCounterName + '&ns_type=hidden' + classTab + hreflang);
        //ns_onclick (this,'','countername','clickin')
    });

    //$("div[class^='simpleTabs']").simpleTabs(); //Run function on any div with class name of "Simple Tabs"
    //Added by Rajeev Dubey to handle tab navigation on career page
    $("ul.tabbed li .tabheadernavigation").click(function (event) {
        event.preventDefault();
        var simpleTabs = $(this).closest(".simpleTabs");
        $(this).closest("ul.tabbed").find("li.activeTab").removeClass("activeTab"); //Remove any "active" class
        $(this).parent().addClass("activeTab"); //Add "active" class to selected tab
        simpleTabs.find(".tabContent").hide(); //Hide all tab content
        var activeTab = $(this).parent().find("a").attr("href"); //Find the rel attribute value to identify the active tab + content

        var $content = $('#content');
        var $tabs = $content.children();
        $tabs.show();

        var classTab = activeTab.replace("#", ".");
        simpleTabs.find(classTab).show(); //Fade in the active content      

        simpleTabs.find(classTab + ' a:first').focus();

        simpleTabs.find(classTab + " .simpleTabs .activeTab .tabheadernavigation").click();
        var hreflang = $(this).parent().find("a").attr('hreflang');
        if (hreflang !== undefined) {
            hreflang = '&amp;cc_language=' + hreflang;
        }
        else {
            hreflang = '';
        }

        classTab = '&amp;cc_tab=' + classTab.replace(".", "");
        sitestat(cliffordChanceCounterName + '&ns_type=hidden' + classTab + hreflang);

        var hashLoc = window.location.hash;
        var hashTab = hashLoc.substring(0, 3);
        var hashComp = hashLoc.substring(3, hashLoc.length);
        var activeComp = activeTab.replace("#", "");
        if (hashComp == activeComp) {
            window.location.hash = hashTab + activeComp;
        } else {
            window.location.hash = '#/1' + activeComp;
        }
    });
    //End Addition
});


/************* FEATURE TOPIC ********/
var FEATURE_TOPIC_HASH = "#FeatureTopic";

function showFeatureTopic() {
    $(".showfeaturetopic").click();

}

$(document).ready(function () {
    $(".block-form-btn").click(function (event) {
        event.preventDefault();
        var href = $(this).closest("form").find("select").val();
        if (href != '')
            window.location = href;
    });
});


/**********Replace SPAN with BLOCKQUOTES************************************/
jQuery.fn.replaceWith = function (replacement) {

    return this.each(function () {
        element = $(this);

        $(this)
            .after(replacement).next()
            .attr('class', element.attr('class')).attr('id', element.attr('id'))
            .html(element.html())
            .prev().remove();
        // alert(element.html());
    });
};
/*******************Email Window Open Function add by Mohit Pal singh on 26/11/2010*****************/

//START :: CHG1041771 :: Email Subject should be editable in CMS
function EmailMe(name, subject) {
    var EmailId;
    var atSign = "@";
    var fullStop = ".";
    var domainName = "cliffordchance" + fullStop + "com";
    EmailId = name + atSign;
    EmailId = EmailId + domainName;
    var EmailIdb = 'mail' + 'to:' + EmailId + '?subject='+subject;
    window.location.href = EmailIdb;
}
//END :: CHG1041771 :: Email Subject should be editable in CMS

/*******************Function Added by Rajeev Dubey to keep tab brosing history on 22/02/2011*****************/
$(function () {
    // Fetch the elements
    var $result = $('#result'),
		$menu = $('#menu'),
		$content = $('#content'),
		$maincontent = $('#overcontent'),
		$1careerdetail = $('#1careerdetail'),
		$2careerdetail = $('#2careerdetail'),
		$3careerdetail = $('#3careerdetail'),
		$4careerdetail = $('#4careerdetail'),
		$mediacenter1 = $('#mediacenter1'),
		$mediacenter2 = $('#mediacenter2'),
		$mediacenter3 = $('#mediacenter3'),
		$featuretopic1 = $('#featuretopic1'),
		$featuretopic2 = $('#featuretopic2'),
		$peopledirectory1 = $('peopledirectory1'),
		$peopledirectory2 = $('#peopledirectory2'),
		$recentawards1 = $('#recentawards1'),
		$recentawards2 = $('#recentawards2'),
		$recentawards3 = $('#recentawards3'),
		$recentawards4 = $('#recentawards4'),
		$forthcomingeventsreg1 = $('#forthcomingeventsreg1'),
		$forthcomingeventsreg2 = $('#forthcomingeventsreg2'),
		$forthcomingeventsreg3 = $('#forthcomingeventsreg3'),
		$forthcomingeventsreg4 = $('#forthcomingeventsreg4');

    var classTab = '&amp;cc_tab=';
    var siteTab = '';


    // Get the tabs
    var $tabs = $content.children();
    var $maintabs = $maincontent.children();

    // Hide all our tabs initially
    $tabs.hide();
    // Fetch our original document title
    var document_title = document.title;

    // Define our update menu function - as this is used a lot
    var updateMenu = function (state) {
        // Update out tab menu
        $menu.children('li:has(a[href="#' + state + '"])').addClass('activeTab').siblings('.activeTab').removeClass('activeTab');
        siteTab = classTab + state.replace("#", "");
        sitestat(cliffordChanceCounterName + '&ns_type=hidden' + siteTab);
    };
    // Bind a handler for ALL hash/state changes
    $.History.bind(function (state) {
        // Update the page's title with our current state on the end
        document.title = document_title + ' | ' + state;
    });
	// START :: CHG1039482 :: Modify back button navigation.
    // Bind a handler for state: 1careerdetail
    $.History.bind('/1careerdetail', function (state) {
        // Update Menu
        updateMenu(state);
        // Show 1careerdetail tab, hide the other tabs
        $("a.tabheadernavigation[hreflang='en']").click(); //Added by Rajeev Dubey to handle tab navigation on career page
        $tabs.hide();
        $maintabs.show();
        $1careerdetail.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: 2careerdetail
    $.History.bind('/2careerdetail', function (state) {
        // Update Menu
        updateMenu(state);
        // Show 2careerdetail tab, hide the other tabs
        $("a.tabheadernavigation[hreflang='en']").click(); //Added by Rajeev Dubey to handle tab navigation on career page
        $tabs.hide();
        $maintabs.hide();
        $2careerdetail.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: 3careerdetail
    $.History.bind('/3careerdetail', function (state) {
        // Update Menu
        updateMenu(state);
        // Show 3careerdetail tab, hide the other tabs
        $("a.tabheadernavigation[hreflang='en']").click(); //Added by Rajeev Dubey to handle tab navigation on career page
        $tabs.hide();
        $maintabs.hide();
        $3careerdetail.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: 4careerdetail
    $.History.bind('/4careerdetail', function (state) {
        // Update Menu
        updateMenu(state);
        // Show 4careerdetail tab, hide the other tabs
        $("a.tabheadernavigation[hreflang='en']").click(); //Added by Rajeev Dubey to handle tab navigation on career page
        $tabs.hide();
        $maintabs.hide();
        $4careerdetail.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: mediacenter1
    $.History.bind('/mediacenter1', function (state) {
        state.replace('#', '');
        // Update Menu
        updateMenu(state);
        // Show mediacenter1 tab, hide the other tabs
        $tabs.hide();
        $maintabs.show();
        $mediacenter1.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: mediacenter2
    $.History.bind('/mediacenter2', function (state) {
        // Update Menu
        updateMenu(state);
        // Show mediacenter2 tab, hide the other tabs
        $tabs.hide();
        $maintabs.hide();
        $mediacenter2.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: mediacenter3
    $.History.bind('/mediacenter3', function (state) {
        // Update Menu
        updateMenu(state);
        // Show mediacenter3 tab, hide the other tabs
        $tabs.hide();
        $maintabs.hide();
        $mediacenter3.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: featuretopic1
    $.History.bind('/featuretopic1', function (state) {
        // Update Menu
        updateMenu(state);
        // Show featuretopic1 tab, hide the other tabs
        $tabs.hide();
        $maintabs.show();
        $('#pubsheader').html('Latest');
        $featuretopic1.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: featuretopic2
    $.History.bind('/featuretopic2', function (state) {
        // Update Menu
        updateMenu(state);
        // Show featuretopic2 tab, hide the other tabs
        $tabs.hide();
        $maintabs.hide();
        $('#pubsheader').html('Feature Topics');
        $featuretopic2.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: peopledirectory1
    $.History.bind('/peopledirectory1', function (state) {
        // Update Menu
        updateMenu(state);
        // Show peopledirectory1 tab, hide the other tabs
        $tabs.hide();
        $maintabs.show();
        $peopledirectory1.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: peopledirectory2
    $.History.bind('/peopledirectory2', function (state) {
        // Update Menu
        updateMenu(state);
        // Show peopledirectory2 tab, hide the other tabs
        $tabs.hide();
        $maintabs.hide();
        $peopledirectory2.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: recentawards1
    $.History.bind('/recentawards1', function (state) {
        // Update Menu
        updateMenu(state);
        // Show recentawards1 tab, hide the other tabs
        $tabs.hide();
        $maintabs.show();
        $recentawards1.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: recentawards2
    $.History.bind('/recentawards2', function (state) {
        // Update Menu
        updateMenu(state);
        // Show recentawards2 tab, hide the other tabs
        $tabs.hide();
        $maintabs.hide();
        $recentawards2.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: recentawards3
    $.History.bind('/recentawards3', function (state) {
        // Update Menu
        updateMenu(state);
        // Show recentawards3 tab, hide the other tabs
        $tabs.hide();
        $maintabs.hide();
        $recentawards3.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: recentawards4
    $.History.bind('/recentawards4', function (state) {
        // Update Menu
        updateMenu(state);
        // Show recentawards4 tab, hide the other tabs
        $tabs.hide();
        $maintabs.hide();
        $recentawards4.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: forthcomingeventsreg1
    $.History.bind('/forthcomingeventsreg1', function (state) {
        // Update Menu
        updateMenu(state);
        // Show forthcomingeventsreg1 tab, hide the other tabs
        $tabs.hide();
        $maintabs.show();
        $forthcomingeventsreg1.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: forthcomingeventsreg2
    $.History.bind('/forthcomingeventsreg2', function (state) {
        // Update Menu
        updateMenu(state);
        // Show forthcomingeventsreg2 tab, hide the other tabs
        $tabs.hide();
        $maintabs.hide();
        $forthcomingeventsreg2.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: forthcomingeventsreg3
    $.History.bind('/forthcomingeventsreg3', function (state) {
        // Update Menu
        updateMenu(state);
        // Show forthcomingeventsreg3 tab, hide the other tabs
        $tabs.hide();
        $maintabs.hide();
        $forthcomingeventsreg3.stop(true, true).customFadeIn(5, function() {
        });
    });

    // Bind a handler for state: forthcomingeventsreg4
    $.History.bind('/forthcomingeventsreg4', function (state) {
        // Update Menu
        updateMenu(state);
        // Show forthcomingeventsreg4 tab, hide the other tabs
        $tabs.hide();
        $maintabs.hide();
        $forthcomingeventsreg4.stop(true, true).customFadeIn(5, function() {
        });
    });
    
	$.fn.customFadeIn = function(speed) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
		});
	};
	// END :: CHG1039482 :: Modify back button navigation.

    $.History.bind('', function (state) {
        history.go(-1);
    });
});
