﻿//Returns random members of an array without duplicates as a string
//call with: genRandomSet(nameofarray,HowManyItemsToReturn).join('\n')
function genRandomSet(src, n) {
	  var dat = src.concat();
	  var max = dat.length-1;
	  var set = [];
	  n = (n<max)? n : max;
	  while (n--) {
	    set.push(dat.splice(Math.random()*dat.length|0, 1));
	  }
	  return set;
}

$(document).ready(function() {
    //Get Current Page Name
    var sPath = window.location.pathname;
    var sLoc = sPath.substring(0, sPath.lastIndexOf('/') + 1);
    var sPage = sLoc.replace(/.aspx/i, "");
    var menuLoc = sLoc + "xml/menuslider.xml";
    // Build an HTML string
    myHTML = '';

    //***********************BIO AND ARTICLE PAGE*******************
    if ((sPath.match("bio_")) || (sPath.match("article_")) || (sPath.match("community"))) {
        //Set curPage accordingly
        if (sPath.match("bio_")) {
            var curPage = "bio";
        } else {
            if (sPath.match("article_")) {
                var curPage = "articles";
            } else {
                var curPage = "bios";
            }
        }
        // Check to see if menuslider has a localized version, if not use global version
        $.ajax({
            url: menuLoc,
            type: 'GET',
            success: function() {
                buildBioArt();
            },
            error: function() {
                menuLoc = '../../en/us/xml/menuslider.xml';
                buildBioArt();
            }
        });
        // Open the menuslider.xml file and check the submenu tags for the 'bios' or 'articles' attribute
        function buildBioArt() {
            $.get(menuLoc, {}, function(xml) {
                $('submenu', xml).each(function(i) {
                    var myList = $(this).attr("myList");
                    //When the articles section is found...
                    if (myList == curPage) {
                        //Strip out extra chars to get current page (eg. article name, bio name)
                        sPage = sPage.replace(/bio_/i, "");
                        sPage = sPage.replace(/article_/i, "");
                        sPage = sPage.replace("_", " ");
                        //Get the children of the xml group
                        var $kids = $(this).children();
                        //Load the children into an array to be randomized or whatever	
                        var myArray = [];
                        $kids.each(function(q) {
                            //Make sure current bio is not included in array
                            if (sPage != ($(this).find("fname").text())) {
                                if (curPage == "bios") {
                                    var newItem = (($(this).find("fname").text()) + "," + ($(this).find("link").text()) + "," + ($(this).find("role").text()) + "," + ($(this).find("location").text()));
                                } else {
                                    var newItem = (($(this).find("fname").text()) + "," + ($(this).find("link").text()));
                                }

                                myArray[myArray.length] = newItem;
                            }
                        });
                        var randList = genRandomSet(myArray, 5).join(';');
                        var randArray = [];
                        randArray = randList.split(";");
                        var tempArray = [];
                        //Build list for side bar
                        if ((curPage == "bio") || (curPage == "article")) {
                            for (var i = 0; i < randArray.length; i++) {
                                tempArray = randArray[i].split(",");
                                if (curPage == "bio") {
                                    myHTML += ('<li><a class="bio" href="' + tempArray[1] + '">' + tempArray[0] + '</a></li>');
                                } else {
                                    myHTML += ('<li><a class="article" href="' + tempArray[1] + '">' + tempArray[0] + '</a></li>');
                                }
                            }
                        } else {
                            //End build list for side bar
                            //build featured bios
                            for (var i = 0; i < 4; i++) {
                                tempArray = randArray[i].split(",");
                                var imgPath = tempArray[0].replace("bio_", "");
                                imgPath = imgPath.replace(".aspx", "");
                                imgPath = imgPath.replace("Featured Bio ", "");
                                imgPath = imgPath.replace(/(\s)/g, '_');
                                //alert(tempArray[2]);
                                myHTML += '<div class="column2"><div style="float: left;"><a href="' + tempArray[1] + '"><img style="margin-bottom: 10px;" src="../../images/bio/' + imgPath + '.png" alt="Photo of ' + tempArray[0] + '" width="90" height="100" /></a></div><div class="spacer-20"></div><div style="float: left;"><ul class="none"><li><a class="bio" href="' + tempArray[1] + '">' + imgPath.replace(/_/g, ' ') + '</a></li><li>' + tempArray[2] + '<br />' + tempArray[3] + '</li></ul></div></div>';
                                //Left Column
                                if (i == 1) {
                                    $("#bioLeft").append(myHTML);
                                    myHTML = "";
                                }
                                //Right Column
                                if (i == 3) {
                                    $("#bioRight").append(myHTML);
                                }
                            }
                        }
                        //end build featured bios
                        if (curPage == "bio") {
                            myHTML += '<li><a href="bios.aspx">More Bios</a></li>';
                            $("#bioList").append(myHTML);
                        }
                        if (curPage == "article") {
                            myHTML += '<li><a href="articles.aspx">More Articles</a></li>';
                            $("#artList").append(myHTML);
                        }
                    }
                });
            });
        }
    }
    //***********************END BIO AND ARTICLE PAGE*******************

    //*************************SET COLOR**************************
    //alert(document.getElementById('setcolor').innerHTML);
    if (document.getElementById('setcolor') != null) {
        var tempArray = [];
        tempArray = (document.getElementById('setcolor').innerHTML).split(",");
        var curColor = tempArray[0];
        tempArray[0] = tempArray[0].replace(/\s/g, "");
        var num = parseInt(tempArray[1]);
        //******************************//
        //Set energy bar color: WITH VIDEO
        var newUrlRight = "../../images/energy/" + curColor + "/energy." + curColor + ".0" + num + ".R.png";
        var newUrlLeft = "../../images/energy/" + curColor + "/energy." + curColor + ".0" + num + ".L.png";
        // IF VIDEO
        if (document.getElementById('videosplash_ebar') != null) {
            document.getElementById('videosplash_ebar').style.backgroundImage = "url('" + newUrlRight + "')";
            document.getElementById('videosplash_ebar').style.backgroundRepeat = "no-repeat";
        }
        //FOOTER
        if (document.getElementById('footer_energy') != null) {
            document.getElementById('footer_energy').innerHTML = "<img src='../../images/energy/" + curColor + "/footer.energy." + curColor + ".png' alt='energybar' width='100%' height='10px' />";
        }



        //IF PORTRAIT
        if (document.getElementById('portraitsplash_ebar') != null) {
            document.getElementById('portraitsplash_ebar').style.backgroundImage = "url('" + newUrlRight + "')";
            document.getElementById('portraitsplash_ebar').style.backgroundRepeat = "no-repeat";
        }

        // LEFT eBAR REPEAT
        if (document.getElementById('ebarrepeat') != null) {
            document.getElementById('ebarrepeat').style.backgroundImage = "url('" + newUrlLeft + "')";
            document.getElementById('ebarrepeat').style.backgroundRepeat = "repeat-x";
        }

        //IF LARGE IMAGE
        if (document.getElementById('LgImg') != null) {
            document.getElementById('LgImg').innerHTML = "<img style='float: left; padding: 0px 30px 0px 0px;' src='../../images/silhouttes/lg/" + curColor + ".0" + num + ".png' />";
        }

        //******************//
        //Featured Case Study 

        var newNum = num;

        for (var i = 1; i < 20; i++) {
            var newFeat = ("feat-" + i);
            if (document.getElementById(newFeat) != null) {
                var oldString = document.getElementById(newFeat).innerHTML;
                var newImage = ("../../images/silhouttes/sm/" + curColor + ".0" + newNum + ".png");
                var replImage = ('../../images/silhouttes/sm/grey.png');
                var newString = oldString.replace(replImage, newImage);

                //var newString = oldString.replace(/images\/silhouttes\/sm\/grey.png/, newImage);
                document.getElementById(newFeat).innerHTML = newString;
                if (newNum == 8) {
                    newNum = 1;
                } else {
                    newNum += 1;
                }
            } else {
                i = 20;
            }
        }
    }
    //END SET COLOR
});


//Search//
function search(){
	var s_local = ('http://www.bing.com/search?q=');
	var s_string = (s_local+document.search.key.value+'+site%3Amicrosoft.com%2Fmicrosoftservices&form=QBRE&qb=2');
	window.location.href=s_string;
}


// print me function //

function print_me(){
printwindow=window.open("print_me.aspx");
}


//***************TEXT BOX HINTS******************//
( function( $ ) {
 
    // plugin definition
    $.fn.overlabel = function( options ) {
 
        // build main options before element iteration
        var opts = $.extend( {}, $.fn.overlabel.defaults, options );
 
        var selection = this.filter( 'label[for]' ).map( function() {
 
            var label = $( this );
            var id = label.attr( 'for' );
            var field = document.getElementById( id );
 
            if ( !field ) return;
 
            // build element specific options
            var o = $.meta ? $.extend( {}, opts, label.data() ) : opts;
 
            label.addClass( o.label_class );
 
            var hide_label = function() { label.css( o.hide_css ) };
            var show_label = function() { this.value || label.css( o.show_css ) };
 
            $( field )
                 .parent().addClass( o.wrapper_class ).end()
                 .focus( hide_label ).blur( show_label ).each( hide_label ).each( show_label );
 
            return this;
 
        } );
 
        return opts.filter ? selection : selection.end();
    };
 
    // publicly accessible defaults
    $.fn.overlabel.defaults = {
 
        label_class:   'overlabel-apply',
        wrapper_class: 'overlabel-wrapper',
        hide_css:      { 'text-indent': '-10000px' },
        show_css:      { 'text-indent': '0px', 'cursor': 'text' },
        filter:        false
 
    };
 
} )( jQuery );
$(document).ready(function() {
    $("label.overlabel").overlabel();
});
