var baseServiceURI = 'http://travelwaysmapper.examiner.com.au/Home.aspx/';

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

$(document).ready(function() {
    $( "#tabs" ).tabs({
        select: function(event, ui) {
            var url = $.data(ui.tab, 'load.tabs');
            if( url ) {
                location.href = url;
                return false;
            }
            return true;
        }
    });                                     
   
	$("#ZoneID").mousedown(function(){
	        if($.browser.msie) {
	                $(this).css("width","auto");  
	        }   
	});
	
	$("#ZoneID").ieSelectStyle();
	
	$("#ZoneID").change(function(){
	        if ($.browser.msie) {
	                $(this).css("width","100%"); 
	        }   
	});
	
	$("#ZoneID").blur(function(){
	        if ($.browser.msie) {
	                $(this).css("width","100%");  
	        } 
	});
	$("#cmbAttZones").mousedown(function(){
	        if($.browser.msie) {
	                $(this).css("width","auto");    
	        }   
	});
	
	$("#cmbAttZones").change(function(){
	        if ($.browser.msie) {
	                $(this).css("width","100%");    
	        }    
	});
	
	$("#cmbAttZones").blur(function(){
	        if ($.browser.msie) {
	                $(this).css("width","100%");    
	        }  
	});

    /* ----------------------------------- Setup the Controls ------------------------------------------------------ */

    $.ajaxSetup({
        cache: false,
        scriptCharset: "UTF-8" ,
        contentType: "application/json; charset=UTF-8"
    });

    $.blockUI.defaults.css.border = '0px';
    $.blockUI.defaults.css.backgroundColor = 'transparent';
    $.blockUI.defaults.overlayCSS.backgroundColor = '#444';
    $.blockUI.defaults.overlayCSS.opacity = "0.2";

    /* Setup Zones */
    /*$('#tabs').block({
        message: '<img src="http://www.travelways.com.au/templates/MASTER/images/ajax-loader.gif" />'
    });       */

    $.ajax({
        url: baseServiceURI + "GetZones/",
        dataType: 'jsonp',
        jsonp: 'callback',
        jsonpCallback: 'zonesComplete'
    });

    /* Setup the  Types */
    /*$('#tabs').block({
        message: '<img src="http://www.travelways.com.au/templates/MASTER/images/ajax-loader.gif" />'
    });   */

    $.ajax({
        url: baseServiceURI + "GetAccommodationTypes",
        dataType: 'jsonp',
        jsonp: 'callback',
        jsonpCallback: 'accomTypesComplete'
    });

    /* Setup the Attraction Types */
    /*$('#tabs').block({
        message: '<img src="http://www.travelways.com.au/templates/MASTER/images/ajax-loader.gif" />'
    });     */        
            
    $.ajax({
        url: baseServiceURI + "GetAttractionTypes",
        dataType: 'jsonp',
        jsonp: 'callback',
        jsonpCallback: 'attrTypesComplete'
    });

    /* Setup Towns when Accommodation Zones change */
    $('#ZoneID').change(function()
    {
        /*$('#tabs').block({
            message: '<img src="http://www.travelways.com.au/templates/MASTER/images/ajax-loader.gif" />'
        }); */
        var selectedZone = $('#ZoneID').val();

        $.ajax({
            url: baseServiceURI + "GetTowns/" + selectedZone,
            dataType: 'jsonp',
            jsonp: 'callback',
            jsonpCallback: 'accomTownsComplete'
        });
    });

    /* Setup Towns when Attraction Zones change */
    $('#cmbAttZones').change(function()
    {
        /*$('#tabs').block({
            message: '<img src="http://www.travelways.com.au/templates/MASTER/images/ajax-loader.gif" />'
        });      */
        var selectedZone = $('#cmbAttZones').val();

        $.ajax({
            url: baseServiceURI + "GetTowns/" + selectedZone,
            dataType: 'jsonp',
            jsonp: 'callback',
            jsonpCallback: 'attrTownsComplete'
        });
    });

    /* Setup the business name to remove the default text on click */
    $('#txtAttBusinessName').focus(function(){
        if($('#txtAttBusinessName').val() == "Business Name")
            $('#txtAttBusinessName').val("");
    });

    $('#txtAccBusinessName').focus(function(){
        $('#txtAccBusinessName').val("");
    });

    $('#txtAttBusinessName').blur(function(){
        if($('#txtAttBusinessName').val() == "")
            $('#txtAttBusinessName').val("Business Name");
    });

    $('#txtAccBusinessName').focus(function(){
        if($('#txtAccBusinessName').val() == "Business Name")
            $('#txtAccBusinessName').val("");
    });

    $('#txtAccBusinessName').blur(function(){
        if($('#txtAccBusinessName').val() == "")
            $('#txtAccBusinessName').val("Business Name");
    });

    /* Setup Date Picker for Arrival Date  */
    $( "#dpAccArrival" ).datepicker({
        dateFormat: 'dd/mm/yy'
    });
    $( "#dpAttArrival" ).datepicker({
        dateFormat: 'dd/mm/yy'
    });

    $("#dpAccArrival").datepicker('setDate', new Date());
    $("#dpAttArrival").datepicker('setDate', new Date());

    $('#submit_button').click(function()
    {
        $.blockUI({
            message: '<img src="http://www.travelways.com.au/templates/MASTER/images/ajax-loader.gif" />'
        });
        var $tabs = $('#tabs').tabs();
        var selected = $tabs.tabs('option', 'selected');

        if(selected == 0){

            if($('#txtAccBusinessName').val() == "Business Name") {
                $('#txtAccBusinessName').val("");
            }

            var searchForm = $("#SearchForm").serialize();
            var searchObject = $("#SearchForm").serializeArray();
            var json = {};

            for (i in searchObject) {
                json[searchObject[i].name] = searchObject[i].value
            }

            $.ajax({
                url: baseServiceURI + "SearchAccommodation/",
                data: searchForm,
                dataType: 'jsonp',
                jsonp: 'callback',
                jsonpCallback: 'searchComplete'
            });
        }
        else if(selected == 1){

            if($('#txtAttBusinessName').val() == "Business Name") {
                $('#txtAttBusinessName').val("");
            }

            var searchForm = $("#AttractionSearchForm").serialize();
            var searchObject = $("#AttractionSearchForm").serializeArray();
            var json = {};

            for (i in searchObject) {
                json[searchObject[i].name] = searchObject[i].value
            }

            $.ajax({
                url: baseServiceURI + "SearchAttractions/",
                data: searchForm,
                dataType: 'jsonp',
                jsonp: 'callback',
                jsonpCallback: 'searchComplete'
            });
        }
    });
});

/* ----------------------------------- Helper Functions ------------------------------------------------------ */
function Post(url, search, results)
{
    $('body').append($('<form/>', {
        id: 'jQueryPostItForm',
        method: 'POST',
        action: url
    }));

    $('#jQueryPostItForm').append($('<input/>', {
        type: 'hidden',
        name: "Search",
        value: search
    }));

    $('#jQueryPostItForm').append($('<input/>', {
        type: 'hidden',
        name: "Data",
        value: results
    }));

    $('#jQueryPostItForm').submit();
}

function searchComplete(data){
    var searchForm = $("#SearchForm").serialize();
    var searchObject = $("#SearchForm").serializeArray();
    var json = {};

    for (i in searchObject) {
        json[searchObject[i].name] = searchObject[i].value
    }

    //$('#tabs').unblock();
    Post("index.php?option=com_travelways&view=results&Itemid=129", JSON.stringify(json), JSON.stringify(data));
}

function zonesComplete(data){

    $('#ZoneID').html("");
    $('#cmbAttZones').html("");
    $(data).each(function(){
        $('#ZoneID').append($('<option></option>').val(this.ID).html(this.Name));
        $('#cmbAttZones').append($('<option></option>').val(this.ID).html(this.Name));
    });

    $.ajax({
        url: baseServiceURI + "GetTowns/" + data[0].ID,
        dataType: 'jsonp',
        jsonp: 'callback',
        jsonpCallback: 'allTownsComplete'
    });

    //$('#tabs').unblock();
}

function allTownsComplete(data){
    accomTownsComplete(data);
    attrTownsComplete(data);
}

function attrTownsComplete(data){
    $('#cmbAttTowns').html("");
    $(data).each(function(intIndex, objValue){
        $('#cmbAttTowns').append(
            $('<option></option>').val(objValue).html(objValue));
    });

    //$('#tabs').unblock();
}

function accomTownsComplete(data){
    $('#cmbAccTowns').html("");
    $(data).each(function(intIndex, objValue){
        $('#cmbAccTowns').append(
            $('<option></option>').val(objValue).html(objValue));

    });

    //$('#tabs').unblock();
}

function accomTypesComplete(data){
    var table = $('#accTypesTable');
    table.html("");

    $(data).each(function(intIndex, objValue){
        table.append($('<tr><td><input id="chkAccomType_' + objValue + '" name="AccommodationType" value="' + objValue + '" type="checkbox" /><label for="chkAccomType_'+ objValue + '">' + objValue + '</label></td></tr>'));
    });

    //$('#tabs').unblock();
}

function attrTypesComplete(data){
    var table = $('#attTypesTable');
    table.html("");

    $(data).each(function(intIndex, objValue){
        table.append($('<tr><td><input id="chkAttractType_' + objValue + '" name="AttractionType" value="' + objValue + '" type="checkbox" /><label for="chkAttractType_'+ objValue + '">' + objValue + '</label></td></tr>'));
    });

    //$('#tabs').unblock();
}
