$(document).ready(function() {
    noShowStr = jQuery('select.location').parents('div:first').attr('rel');
    jQuery('select.location').die('change');
    jQuery('select.location').live('change',function(){
        var locationId = parseInt( jQuery(this).find('option:selected').val());
        var featureCode = "";
        var groupByBookingCode=(jQuery(this).siblings('select.featureDropDownAttributes').val()==undefined)?false:true;
        if(jQuery('select.location').parents('div:first').hasClass('localSchedule'))
        {
            jQuery(this).siblings('input.dropDownSubmit').addClass('disable');
            if(jQuery(this).parents('div.quickPurchase').length>0){
                jQuery(this).parents('div.quickPurchase').find('input.quickPurchaseSubmit').addClass('disable');
            }
            if (jQuery.browser.msie && parseFloat(jQuery.browser.version)<8 ){
                if(!jQuery(this).siblings('select.featuresDropDown').is(':hidden')){
                    jQuery(this).siblings('select.featuresDropDown').hide().show();
                }
                jQuery(this).siblings('select.featureDropDownDates').hide().show();
                jQuery(this).siblings('select.featureDropDownDateTime').hide().show();
            }
            bindFeaturesDatesDropDown();
        }
        else
        {
            jQuery(this).siblings('select.featuresDropDown').find('option').not(':eq(0)').remove();
            jQuery(this).siblings('select.featuresDropDown').find('option:first').html(jQuery(this).siblings('select.featuresDropDown').attr('title'));
            if(groupByBookingCode)
            {
                jQuery(this).siblings('select.featureDropDownAttributes').find('option').not(':eq(0)').remove();
                jQuery(this).siblings('select.featureDropDownAttributes').find('option:first').html(jQuery(this).siblings('select.featureDropDownAttributes').attr('title'));
            }
            jQuery(this).siblings('select.featureDropDownDates').find('option').not(':eq(0)').remove();
            jQuery(this).siblings('select.featureDropDownDates').find('option:first').html(jQuery(this).siblings('select.featureDropDownDates').attr('title'));
            jQuery(this).siblings('select.featureDropDownDateTime').find('option').not(':eq(0)').remove();
            if (jQuery.browser.msie && parseFloat(jQuery.browser.version)<8 ){
                if(!jQuery(this).siblings('select.featuresDropDown').is(':hidden')){
                    jQuery(this).siblings('select.featuresDropDown').hide().show();
                }
                jQuery(this).siblings('select.featureDropDownDates').hide().show();
                jQuery(this).siblings('select.featureDropDownDateTime').hide().show();

            }
            if(locationId==0){
                return false;
            }
            if(jQuery(this).attr('rel')){
                featureCode = jQuery(this).attr('rel');
            }
            var $thisSelect = jQuery(this);
            $thisSelect.siblings('.dropDownLoader').show();
            jQuery.post('dropDownSubLocation',{locationId:locationId,featureCode:featureCode,groupByBookingCode:groupByBookingCode},function(data){
                $thisSelect.siblings('select.featuresDropDown').find('option').not(':eq(0)').remove();
                $thisSelect.siblings('select.featuresDropDown').html($thisSelect.siblings('select.featuresDropDown').html() + data);
                if($thisSelect.siblings('select.featuresDropDown').find('option').length==1){
                    $thisSelect.siblings('select.featuresDropDown').find('option:first').html(noShowStr);
                }else{
                    $thisSelect.siblings('select.featuresDropDown').find('option:first').html($thisSelect.siblings('select.featuresDropDown').attr('title'));
                }
                if (jQuery.browser.msie && parseFloat(jQuery.browser.version)<8 && !$thisSelect.siblings('select.featuresDropDown').is(':hidden') ){
                    $thisSelect.siblings('select.featuresDropDown').hide().show();
                }
                bindFeaturesDropDown();
                $thisSelect.siblings('.dropDownLoader').hide('fast');
            });
        }
    }).find('option:first').attr('selected',true).trigger('change');

    jQuery('div.quickPurchaseWrapper select.featuresDropDown').die('change');
    jQuery('div.quickPurchaseWrapper select.featuresDropDown').live('change',function(){
        var featureId = parseInt( jQuery(this).find('option:selected').val());
        jQuery(this).siblings('select.venuesDropDown').find('option').not(':eq(0)').remove();
        jQuery(this).siblings('select.venuesDropDown').find('option:first').html(jQuery(this).siblings('select.venuesDropDown').attr('title'));

        jQuery(this).siblings('select.featureDropDownDates').find('option').not(':eq(0)').remove();
        jQuery(this).siblings('select.featureDropDownDates').find('option:first').html(jQuery(this).siblings('select.featureDropDownDates').attr('title'));
        jQuery(this).siblings('select.featureDropDownDateTime').find('option').not(':eq(0)').remove();
        if (jQuery.browser.msie && parseFloat(jQuery.browser.version)<8 ){
            if(!jQuery(this).siblings('select.venuesDropDown').is(':hidden')){
                jQuery(this).siblings('select.venuesDropDown').hide().show();
            }
            jQuery(this).siblings('select.featureDropDownDates').hide().show();
            jQuery(this).siblings('select.featureDropDownDateTime').hide().show();

        }
        if(featureId==0){
            return false;
        }

        var $thisSelect = jQuery(this);
        $thisSelect.siblings('.dropDownLoader').show();
        jQuery.post('dropBoxGetFeatureVenues',{featureId:featureId},function(data){
            $thisSelect.siblings('select.venuesDropDown').find('option').not(':eq(0)').remove();
            $thisSelect.siblings('select.venuesDropDown').html($thisSelect.siblings('select.venuesDropDown').html() + data);
            if($thisSelect.siblings('select.venuesDropDown').find('option').length==1){
                $thisSelect.siblings('select.venuesDropDown').find('option:first').html($thisSelect.parents('li:first').attr('rel'));
            }else{
                $thisSelect.siblings('select.venuesDropDown').find('option:first').html($thisSelect.siblings('select.venuesDropDown').attr('title'));
            }
            if (jQuery.browser.msie && parseFloat(jQuery.browser.version)<8 && !$thisSelect.siblings('select.venuesDropDown').is(':hidden') ){
                $thisSelect.siblings('select.venuesDropDown').hide().show();
            }
            bindVenuesDropDown();
            $thisSelect.siblings('.dropDownLoader').hide('fast');
        });

    });

    if(jQuery('select.location option').length==2){
        jQuery('select.location option:last').attr('selected',true);
        jQuery('select.location').trigger('change');
    }
    jQuery('input.dropDownSubmit').live('click',function(){
        if($(this).hasClass('disable')){
            return false;
        }
        var url =jQuery(this).siblings('select.location').find('option:selected').attr('rel');
        var prsntCode =jQuery(this).siblings('select.featureDropDownDateTime').find('option:selected').val();
        if(prsntCode == 0){
            return false;
        }
        url = url.replace("$PrsntCode$",prsntCode);
        if (!$(this).attr('rel')) {
            openFancyBoxExternal(url);
        }else{
            var layoutUrl = $(this).attr('rel');
            var params = "";
            if (layoutUrl==0) {
                layoutUrl=url;
            } else {
                if (layoutUrl.indexOf("?") >= 0) {
                    layoutUrl = layoutUrl.split("?")[0];
                    params = $(this).attr('rel').split("?")[1].split("=")[0];
                } else {
                    layoutUrl = layoutUrl.replace("$URL$", url);
                }
            }
            $('body').append("<form style='display: none;' method='post' id='postForm' action='"+layoutUrl+"'></form");
            if(params.length>0){
                $('#postForm').append("<input type='hidden' name='"+params+"' value='"+url+"'>");
            }
            $('#postForm').submit();
        }
    });
    jQuery('div.quickPurchase input.dropDownSubmit').remove();
    jQuery('div.quickPurchase .dropDownLoader').hide();
    jQuery('div.quickPurchase input.quickPurchaseSubmit').live('click',function(){
        if($(this).hasClass('disable')){
            return false;
        }
        var url =jQuery(this).parents('.quickPurchase').attr('rel');
        var selectedDateTime = jQuery(this).parent().siblings('.ui-tabs-panel').not('.ui-tabs-hide').find('select.featureDropDownDateTime').find('option:selected');
        var prsntCode = selectedDateTime.val();
        var siteId =selectedDateTime.attr('rel');
        if(prsntCode == 0 || siteId == 0){
            return false;
        }
        url = url.replace("$PrsntCode$",prsntCode).replace("$SiteId$",siteId);
        if (!$(this).attr('rel')) {
            openFancyBoxExternal(url);
        }else{
            var layoutUrl = $(this).attr('rel');
            var params = "";
            if(layoutUrl==0){
                layoutUrl = url;
            }else{
                if(layoutUrl.indexOf("?")>=0){
                    layoutUrl = layoutUrl.split("?")[0];
                    params = $(this).attr('rel').split("?")[1].split("=")[0];
                }else{
                    layoutUrl = layoutUrl.replace("$URL$",url);
                }
            }
            $('body').append("<form style='display: none;' method='post' id='postForm' action='"+layoutUrl+"'></form");
            if(params.length>0){
                $('#postForm').append("<input type='hidden' name='"+params+"' value='"+url+"'>");
            }
            $('#postForm').submit();
        }
    });
});

function bindFeaturesDropDownAttributes(e){
    jQuery('select.featureDropDownAttributes').die('change');
    jQuery('select.featureDropDownAttributes').live('change',function(){
        var locationId = jQuery(this).siblings('select.location').find("option:selected").val();
        var featureId = jQuery(this).find('option:selected').val();
        jQuery(this).siblings('select.featureDropDownDates').find('option').not(':eq(0)').remove();
        jQuery(this).siblings('select.featureDropDownDates').find('option:first').html(jQuery(this).siblings('select.featureDropDownDates').attr('title'));
        jQuery(this).siblings('select.featureDropDownDateTime').find('option').not(':eq(0)').remove();
        jQuery(this).siblings('input.dropDownSubmit').addClass('disable');
        if(jQuery(this).parents('div.quickPurchase').length>0){
            jQuery(this).parents('div.quickPurchase').find('input.quickPurchaseSubmit').addClass('disable');
        }
        if( featureId==0 || locationId==0){
            return false;
        }
        var $thisSelect = jQuery(this);
        jQuery.post('dropBoxSubmitFeature',{locationId: locationId,featureId:featureId,extraString:jQuery(e).attr('rel')},function(data){
            $thisSelect.siblings('select.featureDropDownDates').find('option').not(':eq(0)').remove();
            $thisSelect.siblings('select.featureDropDownDates').html($thisSelect.siblings('select.featureDropDownDates').html() + data);
            if($thisSelect.siblings('select.featureDropDownDates').find('option').length==1){
                $thisSelect.siblings('select.featureDropDownDates').find('option:first').html(noShowStr);
            }else{
                $thisSelect.siblings('select.featureDropDownDates').find('option:first').html($thisSelect.siblings('select.featureDropDownDates').attr('title'));
            }
            if (jQuery.browser.msie && parseFloat(jQuery.browser.version)<8){
                $thisSelect.siblings('select.featureDropDownDates').hide().show();
            }
            bindFeaturesDatesDropDown();
            $thisSelect.siblings('.dropDownLoader').hide('fast');
        });
    });
    jQuery('select.featureDropDownAttributes').each(function(i,currSel){
        if(jQuery(currSel).find('option').length==2){
            jQuery(currSel).find('option:last').attr('selected',true);
            jQuery(currSel).trigger('change');
        }
    });
}


function bindFeaturesDropDown(){
    jQuery('select.featuresDropDown').die('change');
    jQuery('select.featuresDropDown').live('change',function(){
        if($(this).parents('.quickPurchaseWrapper').length>0) return false;
        var locationId = jQuery(this).siblings('select.location').find("option:selected").val();
        var featureId = jQuery(this).find("option:selected").val().split('_')[0];
        jQuery(this).siblings('select.featureDropDownDates').find('option').not(':eq(0)').remove();
        jQuery(this).siblings('select.featureDropDownDates').find('option:first').html(jQuery(this).siblings('select.featureDropDownDates').attr('title'));
        jQuery(this).siblings('select.featureDropDownDateTime').find('option').not(':eq(0)').remove();
        jQuery(this).siblings('input.dropDownSubmit').addClass('disable');
        if(jQuery(this).parents('div.quickPurchase').length>0){
            jQuery(this).parents('div.quickPurchase').find('input.quickPurchaseSubmit').addClass('disable');
        }
        if( featureId==0 || locationId==0){
            return false;
        }
        var groupByBookingCode=(jQuery(this).siblings('select.featureDropDownAttributes').val()==undefined)?false:true;
        if(groupByBookingCode)
        {
            jQuery(this).siblings('select.featureDropDownAttributes').find('option').not(':eq(0)').remove();
            jQuery(this).siblings('select.featureDropDownAttributes').find('option:first').html(jQuery(this).siblings('select.featureDropDownAttributes').attr('title'));
            if(jQuery(this).find('option:selected').val().split('_').length==2)
            {
                var i,j;
                var attr_val=jQuery(this).siblings('select.featureDropDownAttributes').attr('rel').split(',');
                var attr=jQuery(this).find('option:selected').val().split('_')[1].split('-');
                var options="";
                for(j=0;j<attr_val.length;j++){
                    for(i=0;i<attr.length-1;i++)
                    {
                        if(attr[i].split('#')[0]==attr_val[j].split('#')[0])
                        {
                            options+='<option value="'+attr[i].split('#')[1]+'">'+attr_val[j].split('#')[1]+'</option>';
                        }
                    }
                }
                jQuery(this).siblings('select.featureDropDownAttributes').html(jQuery(this).siblings('select.featureDropDownAttributes').html()+options);
            }
            bindFeaturesDropDownAttributes(this);
        }
        else
        {
            var $thisSelect = jQuery(this);
            $thisSelect.siblings('.dropDownLoader').show();
            jQuery.post('dropBoxSubmitFeature',{locationId: locationId,featureId:featureId,extraString:jQuery(this).attr('rel')},function(data){
                $thisSelect.siblings('select.featureDropDownDates').find('option').not(':eq(0)').remove();
                $thisSelect.siblings('select.featureDropDownDates').html($thisSelect.siblings('select.featureDropDownDates').html() + data);
                if($thisSelect.siblings('select.featureDropDownDates').find('option').length==1){
                    $thisSelect.siblings('select.featureDropDownDates').find('option:first').html(noShowStr);
                }else{
                    $thisSelect.siblings('select.featureDropDownDates').find('option:first').html($thisSelect.siblings('select.featureDropDownDates').attr('title'));
                }
                if (jQuery.browser.msie && parseFloat(jQuery.browser.version)<8){
                    $thisSelect.siblings('select.featureDropDownDates').hide().show();
                }
                bindFeaturesDatesDropDown();
                $thisSelect.siblings('.dropDownLoader').hide('fast');
            });
        }
    });

    jQuery('select.featuresDropDown').each(function(i,currSel){
        if(jQuery(currSel).css('display')!='none' && jQuery('select.featuresDropDown').length >1 ){

        }else{
            if(jQuery(currSel).find('option').length==2){
                jQuery(currSel).find('option:last').attr('selected',true);
                jQuery(currSel).trigger('change');
            }
        }
    });
}

function bindVenuesDropDown(){
    jQuery('select.venuesDropDown').die('change');
    jQuery('select.venuesDropDown').live('change',function(){
        var siteId = 0;
        var featureId =0;
        var venueId =0;
        if (jQuery(this).find("option:selected").val().indexOf("_")>=0) {
            siteId = jQuery(this).find("option:selected").val().split('_')[0];
            venueId = jQuery(this).find("option:selected").val().split('_')[1];
        }
        featureId = jQuery(this).siblings('select.featuresDropDown').find('option:selected').val();
        jQuery(this).siblings('select.featureDropDownDates').find('option').not(':eq(0)').remove();
        jQuery(this).siblings('select.featureDropDownDates').find('option:first').html(jQuery(this).siblings('select.featureDropDownDates').attr('title'));
        jQuery(this).siblings('select.featureDropDownDateTime').find('option').not(':eq(0)').remove();
        jQuery(this).siblings('input.dropDownSubmit').addClass('disable');
        if(jQuery(this).parents('div.quickPurchase').length>0){
            jQuery(this).parents('div.quickPurchase').find('input.quickPurchaseSubmit').addClass('disable');
        }
        if( featureId==0 || siteId==0 || venueId==0){
            return false;
        }

        var $thisSelect = jQuery(this);
        $thisSelect.siblings('.dropDownLoader').show();
        jQuery.post('dropBoxSubmitVenue',{siteId: siteId,featureId:featureId,venueId:venueId,extraString:jQuery(this).attr('rel')},function(data){
            $thisSelect.siblings('select.featureDropDownDates').find('option').not(':eq(0)').remove();
            $thisSelect.siblings('select.featureDropDownDates').html($thisSelect.siblings('select.featureDropDownDates').html() + data);
            if($thisSelect.siblings('select.featureDropDownDates').find('option').length==1){
                $thisSelect.siblings('select.featureDropDownDates').find('option:first').html(noShowStr);
            }else{
                $thisSelect.siblings('select.featureDropDownDates').find('option:first').html($thisSelect.siblings('select.featureDropDownDates').attr('title'));
            }
            if (jQuery.browser.msie && parseFloat(jQuery.browser.version)<8){
                $thisSelect.siblings('select.featureDropDownDates').hide().show();
            }
            bindFeaturesDatesDropDown();
            $thisSelect.siblings('.dropDownLoader').hide('fast');
        });

    });

    jQuery('select.featuresDropDown').each(function(i,currSel){
        if(jQuery(currSel).css('display')!='none' && jQuery('select.featuresDropDown').length >1 ){

        }else{
            if(jQuery(currSel).find('option').length==2){
                jQuery(currSel).find('option:last').attr('selected',true);
                jQuery(currSel).trigger('change');
            }
        }
    });
}

function bindFeaturesDatesDropDown(){

    jQuery('select.featureDropDownDates').die('change');
    jQuery('select.featureDropDownDates').live('change',function(){
        var siteId = 0;
        var featureId = 0;
        if(jQuery(this).prev().hasClass('featuresDropDown') || jQuery(this).prev().prev().hasClass('featuresDropDown')){
            featureId = jQuery(this).siblings('select.featuresDropDown').find('option:selected').val().split('_')[0];
            var groupByBookingCode=(jQuery(this).siblings('select.featureDropDownAttributes').val()==undefined)?false:true;
            if(groupByBookingCode)
            {
                featureId=jQuery(this).siblings('select.featureDropDownAttributes').find('option:selected').val();
            }
        }else{
            if (jQuery(this).siblings('select.venuesDropDown').find("option:selected").val().indexOf("_")>=0) {
                siteId = jQuery(this).siblings('select.venuesDropDown').find("option:selected").val().split('_')[0];
                featureId = jQuery(this).siblings('select.featuresDropDown').find("option:selected").val();
            }
        }
        var date = jQuery(this).find("option:selected").val();
        jQuery(this).siblings('select.featureDropDownDateTime').find('option').not(':eq(0)').remove();
        jQuery(this).siblings('input.dropDownSubmit').addClass('disable');
        if(jQuery(this).parents('div.quickPurchase').length>0){
            jQuery(this).parents('div.quickPurchase').find('input.quickPurchaseSubmit').addClass('disable');
        }
        if( date==0){
            return false;
        }
        var data ="";
        if (siteId==0) {
            data =featuresSchedule[featureId][date];
        }else{
            data =featuresSchedule[siteId+"_"+featureId][date];
        }
        jQuery(this).siblings('select.featureDropDownDateTime').find('option').not(':eq(0)').remove();
        jQuery(this).siblings('select.featureDropDownDateTime').html(jQuery(this).siblings('select.featureDropDownDateTime').html() + data);
        if (jQuery.browser.msie && parseFloat(jQuery.browser.version)<8){
            jQuery(this).siblings('select.featureDropDownDateTime').hide().show();
        }
        bindFeaturesDatesTimeDropDown();
    });

    if(jQuery('select.featureDropDownDates option').length==2){
        jQuery('select.featureDropDownDates option:last').attr('selected',true);
        jQuery('select.featureDropDownDates').trigger('change');
    }
}

function bindFeaturesDatesTimeDropDown(){
    jQuery('select.featureDropDownDateTime').die('change');
    jQuery('select.featureDropDownDateTime').live('change',function(){
        var date = jQuery(this).find("option:selected").val();
        if( date==0){
            jQuery(this).siblings('input.dropDownSubmit').addClass('disable');
            if(jQuery(this).parents('div.quickPurchase').length>0){
                jQuery(this).parents('div.quickPurchase').find('input.quickPurchaseSubmit').addClass('disable');
            }
            return false;
        }
        jQuery(this).siblings('input.dropDownSubmit').removeClass('disable');
        if(jQuery(this).parents('div.quickPurchase').length>0){
            jQuery(this).parents('div.quickPurchase').find('input.quickPurchaseSubmit').removeClass('disable');
        }
    });

    if(jQuery('select.featureDropDownDateTime option').length==2){
        jQuery('select.featureDropDownDateTime option:last').attr('selected',true);
        jQuery('select.featureDropDownDateTime').trigger('change');
    }
}
