/*
	Notice about purchased source code of SelectboxReplacement.JS from Ascent Media Corporation
	-------------------------------------------------------------------------------------------
	This notice is to confirm that the source code of SelectboxReplacement.JS
	developed by Martin Ivanov (email: acidmartin@yahoo.com; web: http://acidmartin.com; phone number: +359899701635)
	has been purchased by Ascent Media Corporation (http://www.ascentmedia.com/) with the permission
	of its author.
	1. I, Martin Ivanov authorize and entitle Ascent Media Corporation to free modification,
	extending and using the script on public websites for their own purposes.
	2. With regards to the code obfuscation on public websites - it is a right of
	Ascent Media Corporation to decide the form in which the script will be
	delivered to the end users. Since the source code has been purchased by Ascent Media Corporation,
	I do not have claims about the form the script will be delivered - either as open source or
	obfuscated. If Ascent Media Corporation decides to deliver the code obfuscated on public
	websites, I recommend either Dean Edwards' Packer (http://dean.edwards.name/packer/) or the
	code obfuscator integrated in the IDE of the plaftorm that Ascent Media Corporation is using.
	3. Altough the source code of SelectboxReplacement.JS has been purchased by Ascent Media Corporation, the author
	retains the copyright of the script, which allows him to further develop and sell the control
	on his or other websites, as the script remains his intellectual property.
	Martin Ivanov
	http://acidmartin.com
	acidmartin@yahoo.com
	+359899701635
*/

var srjsSkin = 'Blue'; // Set skin to SelectBoxReplacement.JS
// Changes related to path
var srjsFolder = requestContext + '/js/acid-martin/1.1/Scripts'; // Root folder of SelectBoxReplacement.JS
var srjsEnabled = true; // Is SelectBoxReplacement.JS enabled or not? (true | false)
var srjsItemScrolling = true; // Set scrolling for longer <option>...</option> lists
var srjsOptionsListHeight = '120'; // Max height of the options list. Working if srjsItemScrolling is set to "true"


function replaceSelectBoxes(){
    replaceSelectBoxesLocal();
}
var replaceSelectBoxesLocal=function(){
    var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
    var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    var isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
    if(srjsEnabled && !isIE6)
    {
        var srjsGUID = '{c61b2050-0d43-11de-8c30-0800200c9a66}';
        var head = $tag('head')[0];
        var selectboxes = $$('select');
        var srjscss = $create('link');
        var srskin = $create('link');

        srjscss.setAttribute('type', 'text/css');
        srjscss.setAttribute('rel', 'stylesheet');
        srjscss.setAttribute('id', 'selectBoxReplacementJsBaseStylesheet');
        srjscss.setAttribute('href', srjsFolder + '/SelectBoxReplacement.JS/Scripts/SelectBoxReplacementJS.css');
        srskin.setAttribute('type', 'text/css');
        srskin.setAttribute('rel', 'stylesheet');
        srskin.setAttribute('id', 'selectBoxReplacementSkin_' + srjsSkin);
        srskin.setAttribute('href', srjsFolder + '/SelectBoxReplacement.JS/Skins/' + srjsSkin + '/Styles.css');

        if($('selectBoxReplacementJsBaseStylesheet') == null)
        {
            head.appendChild(srjscss);
        }
        if($('selectBoxReplacementSkin_' + srjsSkin) == null)
        {
            head.appendChild(srskin);
        }

        for(i = 0; i < selectboxes.length; i ++)
        {
            if((!selectboxes[i].className || selectboxes[i].className.endsWith("Skin")) && selectboxes[i].size <=1)
            {
                //Changes skin for the select menu if necessary, otherwise sets the skin to the Blue default
                srjsSkin = 'Blue';
                if(selectboxes[i].hasClassName("blackSkin")){
                    srjsSkin = 'Black';
                }

                var isIE = window.ActiveXObject;
                var skinnedselectbox = $create('div');
                var skinnedselectboxoptions = $create('div');
                var skinnedselectboxoptionslist = $create('ul');
                var selectoptions = selectboxes[i].options;
                var selectboxeswidth = selectboxes[i].style.width;// - parseInt(1);
                if(selectboxeswidth){
                    var t = selectboxeswidth.indexOf('px', 0);
                    if(t > 0){
                        selectboxeswidth = selectboxeswidth.substr(0, t);
                        selectboxeswidth = selectboxeswidth - parseInt(1);
                    }
                }else{
                    selectboxeswidth = '250';
                }
                var headerDivWidth = selectboxeswidth-20;
                var selectboxtitle = selectboxes[i].title;
                var selectboxdisabled = selectboxes[i].getAttribute('disabled');
                var selectboxid = selectboxes[i].getAttribute('id');
                var selectboxlabel = selectboxes[i].previousSibling;

                if(isIE)
                {
                    skinnedselectbox.setAttribute('unselectable', 'on');
                }
                if(!selectboxes[i].disabled)
                {
                    skinnedselectbox.className = 'srjsWrapper srjs_' + srjsSkin;
                }
                else
                {
                    skinnedselectbox.className = 'srjsWrapper srjs_' + srjsSkin + ' srjsDisabled';
                    if(Prototype.Browser.IE){
                        $(skinnedselectbox).setStyle("background-color:#f1f1f1");
                    }
                }

                if(!selectboxid)
                {
                    skinnedselectbox.setAttribute('id', 'SkinnedSelectBox_' + i);
                }

                else
                {
                    skinnedselectbox.setAttribute('id', selectboxid + '_SkinnedSelectBox');
                }
                var skinnedselectboxtitle;
                if(selectboxtitle != '' || selectboxtitle != null)
                {
                    skinnedselectboxtitle = selectboxtitle;
                }

                skinnedselectboxoptions.className = 'srjsOptions';
                //Added to assign ID to options popup.
                skinnedselectboxoptions.id=skinnedselectbox.getAttribute('id')+'_srjsOptions';
                skinnedselectbox.style.width = selectboxeswidth + 'px';

                var fakelectedoption=0;
                for (k = 0; k < selectoptions.length; k++){
                    if(selectoptions[k].selected){
                        fakelectedoption = k;
                    }
                }

                // Added to support the Blank value in the Options
                var selectboxvalue = selectboxes[i].options[fakelectedoption].innerHTML;
                if(selectboxvalue && selectboxvalue == 'ID-BLANK'){
                    selectboxvalue = ' ';
                }
                var selectboxicon = selectboxes[i].options[fakelectedoption].className;
                if(!isIE){
                    var headerDiv = $create('div');//new Element("div",{'class':'srjsHeader','onmouseover':'alert(\'test\')'});
                    headerDiv.className='srjsHeader';
                    var headerLink = new Element("a",{
                        'href':'javascript:void(0);',
                        'title':skinnedselectboxtitle,
                        'class':'srjsSelectBox',
                        'style':'width: ' + headerDivWidth + 'px;',
                        'name' :skinnedselectboxoptionslistitemvalue
                    });
                    var innerData = new Element("span",{'class':selectboxicon});
                    innerData.addClassName(selectboxicon);
                    innerData.update(selectboxvalue);

                    headerLink.appendChild(innerData);
                    headerDiv.appendChild(headerLink);
                    skinnedselectbox.appendChild(headerDiv);
                }else{
                    skinnedselectbox.innerHTML = [
                        '<div class="srjsHeader">' +
                        '<a title="' + skinnedselectboxtitle + '" class="srjsSelectBox" href="javascript:void(0)" style="width: ' + headerDivWidth + 'px;">' +
                        '<span><span class="' + selectboxicon + '">' + selectboxvalue + '</span></span>' +
                        '</a>' +
                        '</div>'
                        ];
                }
                if( selectboxlabel && selectboxlabel.tagName == 'LABEL')
                {
                    var selectboxlabeltext = selectboxlabel.innerHTML;
                    if(window.attachEvent)
                    {
                        selectboxlabel.innerHTML = '<span class="srjsLabel" onclick="this.parentNode.nextSibling.nextSibling.childNodes[0].childNodes[0].click();">' + selectboxlabeltext + '</span>';
                    }
                    else
                    {
                        selectboxlabel.innerHTML = '<span class="srjsLabel">' + selectboxlabeltext + '</span>';
                    }
                }

                skinnedselectbox.appendChild(skinnedselectboxoptions);

                if(srjsGUID != '{c61b2050-0d43-11de-8c30-0800200c9a66}')
                {
                    if(!isChrome && !isSafari)
                    {
                        var trialmessagestylestring = '.trialmessage * {position: static !important; text-indent: 0 !important;} .trialmessage {text-indent: 0 !important; margin: 1px !important; display: block !important; visibility: visible !important; background: infobackground; border: solid 1px black;} .trialmessage a {color: black; text-decoration:underline; text-align: center; text-decoration: none !important; display: block !important; position: static !important; visibility: visible !important;} .trialmessage a span {cursor: hand; cursor: pointer; font-size: 11px; display: block !important; position: static !important; visibility: visible !important;}';
                        var trialmessage = $create('div');
                        trialmessage.className = 'trialmessage';
                        if($('SelectBoxReplacementJsTrialMessage') == null)
                        {
                            var trialmsgcss = $create('style');
                            trialmsgcss.setAttribute('type', 'text/css');
                            trialmsgcss.setAttribute('id', 'SelectBoxReplacementJsTrialMessage');
                            if(!window.ActiveXObject)
                            {
                                trialmsgcss.innerHTML = trialmessagestylestring;
                            }
                            else
                            {
                                trialmsgcss.styleSheet.cssText = trialmessagestylestring;
                            }
                            head.appendChild(trialmsgcss);
                        }
                        trialmessage.innerHTML = [
                        '<a href="javascript:void(0)" onclick="purchaseSelectBoxReplacementJs();" title="Purchase the full version of SelectBoxReplacement.JS"><span>Trial Version</span></a>'
                        ];
                        skinnedselectboxoptions.appendChild(trialmessage);
                    }
                }
                skinnedselectboxoptions.appendChild(skinnedselectboxoptionslist);
                skinnedselectboxoptions.style.width = selectboxeswidth - parseInt(2) + 'px';

                for(j = 0; j < selectoptions.length; j ++)
                {
                    var skinnedselectboxoptionslistitem = $create('li');
                    var skinnedselectboxoptionslistitemtext = selectoptions[j].innerHTML;
                    var skinnedselectboxoptionslistitemvalue = selectoptions[j].value;
                    var titleText=skinnedselectboxoptionslistitemtext;
                    //Added to support blank value in the Options
                    if(skinnedselectboxoptionslistitemvalue && skinnedselectboxoptionslistitemvalue == 'ID-BLANK'){
                        skinnedselectboxoptionslistitemtext = '&#160;';
                    }
                    if(skinnedselectboxoptionslistitemtext && skinnedselectboxoptionslistitemtext == 'ID-BLANK'){
                        skinnedselectboxoptionslistitemtext = '&#160;';
                    }
                    var skinnedselectboxoptionicon = selectoptions[j].className;
                    // Added to support disabled behaviour of Option
                    var skinnedselectboxoptionDisabled = selectoptions[j].getAttribute('disabled');

                    if(isIE){
                        if(skinnedselectboxoptionDisabled){
                            skinnedselectboxoptionslistitem.innerHTML = [
                            '<a href="javascript:void(0)" name="' + skinnedselectboxoptionslistitemvalue + '">' +
                            '<span class="srjsInnerSpanDisabled ' + skinnedselectboxoptionicon + '">' + skinnedselectboxoptionslistitemtext + '</span>' +
                            '</a>'
                            ];
                        }else{
                            skinnedselectboxoptionslistitem.innerHTML = [
                            '<a href="javascript:void(0)" onclick="setValue(this, ' + j + ');" name="' + skinnedselectboxoptionslistitemvalue + '">' +
                            '<span class="srjsInnerSpan ' + skinnedselectboxoptionicon + '" title="' + titleText + '">' + skinnedselectboxoptionslistitemtext + '</span>' +
                            '</a>'
                            ];
                        }
                    }else{
                        //DIEGO:  changed the innerHtml creation of the elements because it has problems with safari with elements that have ampersands.
                        var listItemText;
                        var listItemLink;
                        listItemText = new Element("span",{});
                        listItemText.addClassName(skinnedselectboxoptionicon);
                        listItemText.update(skinnedselectboxoptionslistitemtext);

                        if(skinnedselectboxoptionDisabled){
                            listItemLink = new Element("a",{
                                'href':'javascript:void(0);',
                                'name' :skinnedselectboxoptionslistitemvalue
                            });
                            listItemText.addClassName("srjsInnerSpanDisabled");
                        }else{
                            listItemLink = new Element("a",{
                                'href':'javascript:void(0);',
                                'name' :skinnedselectboxoptionslistitemvalue,
                                'title':titleText,
                                'onclick':'setValue(this,'+j+')'
                            });
                            listItemText.addClassName("srjsInnerSpan");
                        }
                        listItemLink.appendChild(listItemText);
                        skinnedselectboxoptionslistitem.appendChild(listItemLink);
                    }
                    skinnedselectboxoptionslist.appendChild(skinnedselectboxoptionslistitem);
                }
                //DIEGO: end of changes for Safari


                var skinnedoptions = skinnedselectboxoptionslist.getElementsByTagName('a');
                skinnedoptions[fakelectedoption].className = 'srjsOptionSelected';
                /*
                     * The JavaScript method insertAdjacentElement() only works in Internet
                     * Explorer (IE) and it is NOT supported in FireFox. Therefore, I(Deepak)
                     * have commented the original code and replaced this line of code with
                     * prototype JS code.
                     */
                //selectboxes[i].insertAdjacentElement('afterEnd', skinnedselectbox);
                Element.insert(selectboxes[i], {
                    after: skinnedselectbox
                });

                if(isIE)
                {
                    var allinnerelements = skinnedselectbox.getElementsByTagName('*');
                    for(l = 0; l < allinnerelements.length; l ++)
                    {
                        allinnerelements[l].setAttribute('unselectable', 'on');
                    }
                }
                selectboxes[i].className = 'srjsRealSelect';
                if(srjsItemScrolling)
                {
                    var optionslistheight = skinnedselectboxoptions.offsetHeight;
                    if(optionslistheight > srjsOptionsListHeight)
                    {
                        skinnedselectboxoptions.style.overflowY = 'scroll';
                        skinnedselectboxoptions.style.height = srjsOptionsListHeight + 'px';
                    }
                }
                skinnedselectboxoptions.style.display = 'none';
		toggleSkinnedSelectBoxOptions();
            }
        }
    }
};

function toggleSkinnedSelectBoxOptions()
{
    $$('div.srjsHeader').each(function(element){
        element.observe('click', function(it){
            var combooptions = element.parentNode.childNodes[1];
            if(window.ActiveXObject)
            {
                document.documentElement.attachEvent('onmouseup', function(e){
                    var tmp = e.srcElement;// element();
                    if(!(tmp && tmp.tagName == 'DIV' && tmp.className == 'srjsOptions')){
                        combooptions.style.display = 'none';
                    }
                });
            }
            else
            {
                document.documentElement.addEventListener('mouseup', function(e)
                {
                    var tmp = e.element();
                    if(tmp && !tmp.hasClassName('srjsOptions')){
                        combooptions.style.display = 'none';
                    }
                }, false
                );
            }
            combooptions.style.display = (combooptions.style.display == "block" ) ? "" : "block";
        });
    });
}

// New function added to support the selected index element to display in Skined dropdown
function setSkinnedBoxValue(selectBoxId, elIndex){
    try{
        var elementparents =$(selectBoxId+'_srjsOptions');
        var linksoptions = elementparents.getElementsByTagName('a');
        elementparents.parentNode.previousSibling.selectedIndex = elIndex;
        var selectedOptionText = elementparents.parentNode.previousSibling.options[elIndex].innerHTML;
        if(selectedOptionText && selectedOptionText== 'ID-BLANK'){
            selectedOptionText = '&#160';
        }
        var headerSpan=elementparents.parentNode.childNodes[0].childNodes[0].childNodes[0];//.childNodes[0];
        headerSpan.innerHTML =selectedOptionText;
        elementparents.style.display = 'none';
        for(i = 0; i < linksoptions.length; i ++)
        {
            linksoptions[i].className = '';
            var elName=linksoptions[i].name;
            // Added to support default display label to disappear when any of the category selected
            if(elName &&  elName == 'ID-REMOVE'){
                var tmp = linksoptions[i].parentNode;
                tmp.style.display ='none';
            }
            // Added to support default display label to change as Options when any of the category selected
            if(elName &&  elName == 'ID-BLANK'){
                var tmp = linksoptions[i].childNodes;
                tmp = tmp[0];
                tmp.innerHTML='&#160;';
            //elementparents.parentNode.previousSibling.options[0].innerHTML= '';
            //elementparents.parentNode.previousSibling.options[0].text= '';
            }
        }
        elementparents.parentNode.previousSibling.onchange();
    }
    catch (err)
    {
    }
}function setValue(element, elIndex)
{
    var elementparents = element.parentNode.parentNode.parentNode;
    var linksoptions = element.parentNode.parentNode.getElementsByTagName('a');
    var headericon = elementparents.parentNode.childNodes[0].childNodes[0].childNodes[0].childNodes[0];
    elementparents.parentNode.previousSibling.selectedIndex = elIndex;
    var selectedOptionText = elementparents.parentNode.previousSibling.options[elIndex].innerHTML;
    if(selectedOptionText && selectedOptionText== 'ID-BLANK'){
        selectedOptionText = '&#160';
    }
    var headerSpan=elementparents.parentNode.childNodes[0].childNodes[0].childNodes[0];//.childNodes[0];
    if(selectedOptionText=='&#160' && Prototype.Browser.WebKit){
        headerSpan.innerHTML=' ';
    }else{
        headerSpan.innerHTML=selectedOptionText;
    }
    elementparents.style.display = 'none';

    for(i = 0; i < linksoptions.length; i ++)
    {
        linksoptions[i].className = '';
        var elName=linksoptions[i].name;
        // Added to support default display label to disappear when any of the category selected
        if(elName &&  elName == 'ID-REMOVE'){
            var tmp = linksoptions[i].parentNode;
            tmp.style.display ='none';
        }
        // Added to support default display label to change as Options when any of the category selected
        if(elName &&  elName == 'ID-BLANK'){
            var tmp = linksoptions[i].childNodes;
            tmp = tmp[0];
            tmp.innerHTML='&#160;';
        //elementparents.parentNode.previousSibling.options[0].innerHTML= '';
        //elementparents.parentNode.previousSibling.options[0].text= '';
        }
    }
    try
    {
        elementparents.parentNode.previousSibling.onchange();
        element.className = 'srjsOptionSelected';
    //headericon.className = element.childNodes[0].childNodes[0].className; //element.childNodes[0].childNodes[0].className;

    }
    catch (err)
    {
        element.className = 'srjsOptionSelected';
        try{
            headericon.className = element.childNodes[0].className;
        }catch(exp){}
    }
}
if(typeof HTMLElement != "undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
    HTMLElement.prototype.insertAdjacentElement = function(where, parsedNode)
    {
        switch (where)
        {
            case 'afterEnd':
                if (this.nextSibling)
                    this.parentNode.insertBefore(parsedNode, this.nextSibling);
                break;
        }
    }
}
function purchaseSelectBoxReplacementJs()
{
    var purchaseurl = 'http://dev.wemakesites.net/Common/Files/PurchaseSelectBoxReplacementJs.html';
    if(confirm('SelectBoxReplacement.JS Trial Message\n------------------------------------------------\n\nYou are using a trial version of SelectBoxReplacement.JS.\nWould you like to purchase the full version, that comes with full set of 12 skins and does not display this message?\n\nPress "OK" to buy the full version via PayPal for USD30 or "Cancel" to continue using the trial.'))
    {
        window.open(purchaseurl, '_blank');
    }
}
function enableSelectBox(SelectBoxID)
{
    if($(SelectBoxID)) {
        var skinnedselectbox = $(SelectBoxID).nextSibling;
        removeClassName(skinnedselectbox, 'srjsDisabled');
        if(Prototype.Browser.IE){
            $(skinnedselectbox).setStyle("background-color:white");
        }
        $(SelectBoxID).disabled = false;
    }
}
function disableSelectBox(SelectBoxID)
{
    var skinnedselectbox = $(SelectBoxID);
    if(skinnedselectbox){
        skinnedselectbox = skinnedselectbox.nextSibling;
        addClassName(skinnedselectbox, 'srjsDisabled');
        if(Prototype.Browser.IE){
            $(skinnedselectbox).setStyle("background-color:#F9F9F8");
        }
        $(SelectBoxID).disabled = true;
    }
}
function toggleSelectBox(SelectBoxID)
{
    var realselectbox = $(SelectBoxID);
    var skinnedselectbox = $(SelectBoxID).nextSibling;
    if(!realselectbox.disabled)
    {
        realselectbox.disabled = true;
        addClassName(skinnedselectbox, 'srjsDisabled');
    }
    else
    {
        realselectbox.disabled = false;
        removeClassName(skinnedselectbox, 'srjsDisabled');
    }
}
function addClassName(oElm, strClassName)
{
    var strCurrentClass = oElm.className;
    if(!new RegExp(strClassName, "i").test(strCurrentClass))
    {
        oElm.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
    }
}
function removeClassName(oElm, strClassName)
{
    var oClassToRemove = new RegExp((strClassName + "\s?"), "i");
    oElm.className = oElm.className.replace(oClassToRemove, "").replace(/^\s?|\s?$/g, "");
}
function getClick(element)
{
    element.parentNode.previousSibling.click();
}
function $tag(tagName)
{
    return document.getElementsByTagName(tagName);
}
function $create(element)
{
    return document.createElement(element);
}

function removeBoxValue(selectBoxId){
    try{
        var elementparents =$(selectBoxId+'_srjsOptions');
        var linksoptions = elementparents.getElementsByTagName('a');
        elementparents.style.display = 'none';
        for(i = 0; i < linksoptions.length; i ++)
        {
            linksoptions[i].className = '';
            var elName=linksoptions[i].name;
            // Added to support default display label to disappear when any of the category selected
            if(elName &&  elName == 'ID-REMOVE'){
                var tmp = linksoptions[i].parentNode;
                tmp.style.display ='none';
            }
        }
    }
    catch (err)
    {
    }
}