/**
 * jQuery custom selectboxes
 * 
 * Copyright (c) 2008 Krzysztof Suszyński (suszynski.org)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * @version 0.6.1
 * @category visual
 * @package jquery
 * @subpakage ui.selectbox
 * @author Krzysztof Suszyński <k.suszynski@wit.edu.pl>
**/

//customized for ddl current item bkground color
//-----------------------------------------------
var dt = new Date();

var jQueryCurrentValueArr = dt.getFullYear().toString() + dt.getMonth().toString();
var jQueryCurrentValueDept = dt.getFullYear().toString() + dt.getMonth().toString();
 
if (dt.getMonth().toString().length == 1) {
    jQueryCurrentValueArr = dt.getFullYear().toString() + "0" + dt.getMonth().toString();
    jQueryCurrentValueDept = dt.getFullYear().toString() + "0" + dt.getMonth().toString();
}

var jQueryCurrentDateDept = (dt.getDate() + 1).toString();
var jQueryCurrentDateArr = dt.getDate().toString();

var jQueryCurrentAdultCount = 1;
var jQueryCurrentChildCount = 0;
var jQueryCurrentRoomCount = 1;
 
//customized for ddl current item bkground color
//-----------------------------------------------


jQuery.fn.selectbox = function(options) {//to fill dropdown list SetMonth('E');
    /* Default settings */
    var settings = {
        className: 'jquery-selectbox',
        animationSpeed: "normal",
        listboxMaxSize: 10,
        replaceInvisible: false
    };
    var commonClass = 'jquery-custom-selectboxes-replaced';
    var listOpen = false;
    var showList = function(listObj) {
        var selectbox = listObj.parents('.' + settings.className + '');
        listObj.slideDown(settings.animationSpeed, function() {
            listOpen = true;
        });
        selectbox.addClass('selecthover');
        jQuery(document).bind('click', onBlurList);
        return listObj;
    }
    var hideList = function(listObj) {
        var selectbox = listObj.parents('.' + settings.className + '');
        listObj.slideUp(settings.animationSpeed, function() {
            listOpen = false;
            jQuery(this).parents('.' + settings.className + '').removeClass('selecthover');
        });
        jQuery(document).unbind('click', onBlurList);
        return listObj;
    }
    var onBlurList = function(e) {
        var trgt = e.target;
        var currentListElements = jQuery('.' + settings.className + '-list:visible').parent().find('*').andSelf();

        if (jQuery.inArray(trgt, currentListElements) < 0 && listOpen) {
            hideList(jQuery('.' + commonClass + '-list'));
        }
        return false;
    }

    /* Processing settings */
    settings = jQuery.extend(settings, options || {});
    /* Wrapping all passed elements */
    return this.each(function() {
        var _this = jQuery(this);
        if (_this.filter(':visible').length == 0 && !settings.replaceInvisible)
            return;

        //customized for home suite dropdown: STARTS
        var replacement;

        if ($(this).attr('id') == "arrivalDay" || $(this).attr('id') == "departureDay" || $(this).attr('id') == "nadult" || $(this).attr('id') == "nchild" || $(this).attr('id') == "nrooms" || $(this).attr('id') == "Booknow_nadult" || $(this).attr('id') == "Booknow_nchild" || $(this).attr('id') == "Booknow_nroom") {
            replacement = jQuery('<div class="' + settings.className + ' ' + commonClass + '">' +
			    '<div class="' + settings.className + '-moreButtonSmall" />' +
			    '<div class="' + settings.className + '-list ' + commonClass + '-list" />' +
			    '<span class="' + settings.className + '-currentItem" />' +
		    '</div>');
        }
        else {
            replacement = jQuery('<div class="' + settings.className + ' ' + commonClass + '">' +
			    '<div class="' + settings.className + '-moreButton" />' +
			    '<div class="' + settings.className + '-list ' + commonClass + '-list" />' +
			    '<span class="' + settings.className + '-currentItem" />' +
		    '</div>');
        }
        //customized for home suite dropdown: ENDS

        jQuery('option', _this).each(function(k, v) {
            var v = jQuery(v);
            var listElement = '';
            //customized for ddl current item bkground color
            //----------------------------------------------- 
            if ($(_this).attr('id') == "arrivalMonthYear" && v.val() == jQueryCurrentValueArr)
                listElement = jQuery('<span style="background-color:#e3bfaf;" class="' + settings.className + '-item value-' + v.val() + ' item-' + k + '">' + v.text() + '</span>');
            else if ($(_this).attr('id') == "departureMonthYear" && v.val() == jQueryCurrentValueDept)
                listElement = jQuery('<span style="background-color:#e3bfaf;" class="' + settings.className + '-item value-' + v.val() + ' item-' + k + '">' + v.text() + '</span>');
             
             else if ($(_this).attr('id') == "ctl00_cphContent_ddlCountry" && v.val() == jQueryCurrentValueDept)             
                listElement = jQuery('<span style="background-color:#e3bfaf;" class="' + settings.className + '-item value-' + v.val() + ' item-' + k + '">' + v.text() + '</span>'); 
             
             else if ($(_this).attr('id') == "ctl00_cphContent_ddlHowFindWebsite" && v.val() == jQueryCurrentValueDept)             
                listElement = jQuery('<span style="background-color:#e3bfaf;" class="' + settings.className + '-item value-' + v.val() + ' item-' + k + '">' + v.text() + '</span>'); 
             
                
            else if ($(_this).attr('id') == "arrivalDay" && v.val() == jQueryCurrentDateArr)
                listElement = jQuery('<span style="background-color:#e3bfaf;" class="' + settings.className + '-item value-' + v.val() + ' item-' + k + '">' + v.text() + '</span>');
            else if ($(_this).attr('id') == "departureDay" && v.val() == jQueryCurrentDateDept)
                listElement = jQuery('<span style="background-color:#e3bfaf;" class="' + settings.className + '-item value-' + v.val() + ' item-' + k + '">' + v.text() + '</span>');
            else
                listElement = jQuery('<span class="' + settings.className + '-item value-' + v.val() + ' item-' + k + '">' + v.text() + '</span>');


            if ($(_this).attr('id') == "nadult" || $(_this).attr('id') == "Booknow_nadult") {
                replacement.find("div").find("span:contains('1')").css("background-color", "#e3bfaf"); 
            }
            else if ($(_this).attr('id') == "nchild" || $(_this).attr('id') == "Booknow_nchild") {
                replacement.find("div").find("span:contains('0')").css("background-color", "#e3bfaf");  
            }
            else if ($(_this).attr('id') == "nroom" || $(_this).attr('id') == "nrooms") {
                replacement.find("div").find("span:contains('1')").css("background-color", "#e3bfaf");  
            }
            //customized for ddl current item bkground color
            //----------------------------------------------

            //jQuery('<span class="' + settings.className + '-item value-' + v.val() + ' item-' + k + '">' + v.text() + '</span>');
            listElement.click(function() {
                var thisListElement = jQuery(this);
                var thisReplacment = thisListElement.parents('.' + settings.className);
                var thisIndex = thisListElement[0].className.split(' ');
                for (k1 in thisIndex) {
                    if (/^item-[0-9]+$/.test(thisIndex[k1])) {
                        thisIndex = parseInt(thisIndex[k1].replace('item-', ''), 10);
                        break;
                    }
                };

                var thisValue = thisListElement[0].className.split(' ');
                for (k1 in thisValue) {
                    if (/^value-.+$/.test(thisValue[k1])) {
                        thisValue = thisValue[k1].replace('value-', '');
                        break;
                    }
                };

                //customized for ddl current item bkground color
                if ($(_this).attr('id') == "arrivalMonthYear") {
                    jQueryCurrentValueArr = thisValue;
                    jQueryCurrentValueDept = thisValue;

                    jQueryCurrentDateDept = parseInt(jQueryCurrentDateArr, 10) + 1;
                }
                
                 if ($(_this).attr('id') == "ctl00_cphContent_ddlCountry") {
                 
                    jQueryCurrentValueArr = thisValue;
                    jQueryCurrentValueDept = thisValue;

                    jQueryCurrentDateDept = parseInt(jQueryCurrentDateArr, 10) + 1;
                }
                
                  if ($(_this).attr('id') == "ctl00_cphContent_ddlHowFindWebsite") {
                 
                    jQueryCurrentValueArr = thisValue;
                    jQueryCurrentValueDept = thisValue;

                    jQueryCurrentDateDept = parseInt(jQueryCurrentDateArr, 10) + 1;
                }
                
                

                if ($(_this).attr('id') == "departureMonthYear") {

                    jQueryCurrentValueDept = thisValue;
                    if (jQueryCurrentValueArr != jQueryCurrentValueDept)
                        jQueryCurrentDateDept = 1;
                }

                if ($(_this).attr('id') == "arrivalDay") {
                    jQueryCurrentDateArr = thisValue;
                    jQueryCurrentDateDept = parseInt(thisValue, 10) + 1;
                }

                if ($(_this).attr('id') == "departureDay") {
                    jQueryCurrentDateDept = thisValue;

                    for (var i = 1; i <= 31; i++) {
                        replacement.find("span:contains('" + i + "')").css("background-color", "");
                        
                    }

                    thisListElement
					.css("background-color", "#e3bfaf");
                }

                if ($(_this).attr('id') == "nadult" || $(_this).attr('id') == "Booknow_nadult") {

                    for (var i = 1; i <= 4; i++) {
                        replacement.find("span:contains('" + i + "')").css("background-color", "");
                    } 

                    thisListElement
					.css("background-color", "#e3bfaf");
                }
                else if ($(_this).attr('id') == "nchild" || $(_this).attr('id') == "Booknow_nchild") {

                    for (var i = 0; i <= 2; i++) {
                        replacement.find("span:contains('" + i + "')").css("background-color", "");
                    }

                    thisListElement
					.css("background-color", "#e3bfaf");
                }
                else if ($(_this).attr('id') == "nrooms" || $(_this).attr('id') == "Booknow_nroom") {

                    for (var i = 1; i <= 9; i++) {
                        replacement.find("span:contains('" + i + "')").css("background-color", "");
                    }

                    thisListElement
					.css("background-color", "#e3bfaf");
                }
                
                ////////////////////////////////////////////////////////////////////////////////////////
                
                
                 else if ($(_this).attr('id') == "ctl00_cphContent_ddlCountry" || $(_this).attr('id') == "ddlCountry") {

              
                     replacement.find("span:contains('')").css("background-color", "");
                    
                    thisListElement
					.css("background-color", "#e3bfaf");
                }
                
                
                else if ($(_this).attr('id') == "ctl00_cphContent_ddlHowFindWebsite" || $(_this).attr('id') == "ddlHowFindWebsite") {

              
                     replacement.find("span:contains('')").css("background-color", "");
                    
                    thisListElement
					.css("background-color", "#e3bfaf");
                }
                
                else if ($(_this).attr('id') == "ctl00_cphContent_ddlMonth" || $(_this).attr('id') == "ddlMonth") {

              
                     replacement.find("span:contains('')").css("background-color", "");
                    
                    thisListElement
					.css("background-color", "#e3bfaf");
                }
                
                
                 else if ($(_this).attr('id') == "ctl00_cphContent_ddlHowArriving" || $(_this).attr('id') == "ddlHowArriving") {

                        replacement.find("span:contains('Car')").css("background-color", "");
                        replacement.find("span:contains('Train')").css("background-color", "");
                        replacement.find("span:contains('Plane')").css("background-color", "");
                        replacement.find("span:contains('Bus')").css("background-color", "");
                        replacement.find("span:contains('Ferry')").css("background-color", "");
                     

                    thisListElement
					.css("background-color", "#e3bfaf");
                }
                
                ////////////////////////////////////////////////////////////////////////////////////////
                
                
                
                
                
                
                
                else {
                    jQuery(listElement).removeClass("CurrentItemBgcolor");
                }
                
                


                thisReplacment
					.find('.' + settings.className + '-currentItem')
					.text(thisListElement.text())

                thisReplacment
					.find('select')
					.val(thisValue)
					.triggerHandler('change');
                var thisSublist = thisReplacment.find('.' + settings.className + '-list');
                if (thisSublist.filter(":visible").length > 0) {
                    hideList(thisSublist);
                } else {
                    showList(thisSublist);
                }

            }).bind('mouseenter', function() {
                jQuery(this).addClass('listelementhover');
            }).bind('mouseleave', function() {
                jQuery(this).removeClass('listelementhover');
            });
            jQuery('.' + settings.className + '-list', replacement).append(listElement);
            if (v.filter(':selected').length > 0) {
                jQuery('.' + settings.className + '-currentItem', replacement).text(v.text());
            }

        });
        //customized for home suite dropdown starts
        if ($(this).attr('id') == "arrivalDay" || $(this).attr('id') == "departureDay" || $(this).attr('id') == "nadult" || $(this).attr('id') == "nchild" || $(this).attr('id') == "nrooms" || $(this).attr('id') == "Booknow_nadult" || $(this).attr('id') == "Booknow_nchild" || $(this).attr('id') == "Booknow_nroom") {
            replacement.find('.' + settings.className + '-moreButtonSmall').click(function() {
                var thisMoreButton = jQuery(this);
                var otherLists = jQuery('.' + settings.className + '-list')
				    .not(thisMoreButton.siblings('.' + settings.className + '-list'));
                hideList(otherLists);
                var thisList = thisMoreButton.siblings('.' + settings.className + '-list');
                if (thisList.filter(":visible").length > 0) {
                    hideList(thisList);
                } else {
                    showList(thisList);
                }
            }).bind('mouseenter', function() {
                jQuery(this).addClass('morebuttonhover');
            }).bind('mouseleave', function() {
                jQuery(this).removeClass('morebuttonhover');
            });

            replacement.find('.' + settings.className + '-currentItem').click(function() {
                var thisMoreButton = jQuery(this);
                var otherLists = jQuery('.' + settings.className + '-list')
				    .not(thisMoreButton.siblings('.' + settings.className + '-list'));
                hideList(otherLists);
                var thisList = thisMoreButton.siblings('.' + settings.className + '-list');
                if (thisList.filter(":visible").length > 0) {
                    hideList(thisList);
                } else {
                    showList(thisList);
                }
            }).bind('mouseenter', function() {
                jQuery(this).addClass('morebuttonhover');
            }).bind('mouseleave', function() {
                jQuery(this).removeClass('morebuttonhover');
            });
        }
        else {
            replacement.find('.' + settings.className + '-moreButton').click(function() {
                var thisMoreButton = jQuery(this);
                var otherLists = jQuery('.' + settings.className + '-list')
				    .not(thisMoreButton.siblings('.' + settings.className + '-list'));
                hideList(otherLists);
                var thisList = thisMoreButton.siblings('.' + settings.className + '-list');
                if (thisList.filter(":visible").length > 0) {
                    hideList(thisList);
                } else {
                    showList(thisList);
                }
            }).bind('mouseenter', function() {
                jQuery(this).addClass('morebuttonhover');
            }).bind('mouseleave', function() {
                jQuery(this).removeClass('morebuttonhover');
            });

            replacement.find('.' + settings.className + '-currentItem').click(function() {
                var thisMoreButton = jQuery(this);
                var otherLists = jQuery('.' + settings.className + '-list')
				    .not(thisMoreButton.siblings('.' + settings.className + '-list'));
                hideList(otherLists);
                var thisList = thisMoreButton.siblings('.' + settings.className + '-list');
                if (thisList.filter(":visible").length > 0) {
                    hideList(thisList);
                } else {
                    showList(thisList);
                }
            }).bind('mouseenter', function() {
                jQuery(this).addClass('morebuttonhover');
            }).bind('mouseleave', function() {
                jQuery(this).removeClass('morebuttonhover');
            });
        }
        //customized for home suite dropdown ends

        _this.hide().replaceWith(replacement).appendTo(replacement);
        var thisListBox = replacement.find('.' + settings.className + '-list');
        var thisListBoxSize = thisListBox.find('.' + settings.className + '-item').length;
        if (thisListBoxSize > settings.listboxMaxSize)
            thisListBoxSize = settings.listboxMaxSize;
        if (thisListBoxSize == 0)
            thisListBoxSize = 1;
        var thisListBoxWidth = Math.round(_this.width() + 5);
        if (jQuery.browser.safari)
            thisListBoxWidth = thisListBoxWidth * 0.94;
        ///to change the height of dropdown
        //START
        if ($(this).attr('id') == "nadult" || $(this).attr('id') == "Booknow_nadult")
            thisListBoxSize += 1.00;
        else if ($(this).attr('id') == "nchild" || $(this).attr('id') == "Booknow_nchild")
            thisListBoxSize += 0.68;
        else if ($(this).attr('id') == "nrooms" || $(this).attr('id') == "Booknow_nroom")
            thisListBoxSize += 2.10;
             else if ($(this).attr('id') == "ctl00_cphContent_ddlHowFindWebsite" || $(this).attr('id') == "ctl00_cphContent_ddlHowFindWebsite")
            thisListBoxSize += 4.60;
             else if ($(this).attr('id') == "ctl00_cphContent_ddlMonth" || $(this).attr('id') == "ctl00_cphContent_ddlMonth")
            thisListBoxSize += 5.80; 
             else if ($(this).attr('id') == "ctl00_cphContent_ddlHowArriving" || $(this).attr('id') == "ctl00_cphContent_ddlHowArriving")
            thisListBoxSize += 1.00;  
     
        //END

        replacement.css('width', thisListBoxWidth + 'px');
        thisListBox.css({
            width: Math.round(thisListBoxWidth - 5) + 'px',
            height: thisListBoxSize + 'em'
        });
    });
}
jQuery.fn.unselectbox = function(){
	var commonClass = 'jquery-custom-selectboxes-replaced';
	return this.each(function() {
		var selectToRemove = jQuery(this).filter('.' + commonClass);
		selectToRemove.replaceWith(selectToRemove.find('select').show());		
	});
}
