var toggle = 'open';
var add_loc = "";
var postcodesAdded = [""];
var ajax_load = "<center><img src='/resources/global/images/ajax-loader.gif' border='0' alt='working' /></center>";

$(document).ready(function(){
    $(".btn-slide").click(function(){
        $("#searchpanel").slideToggle("fast");
        $(this).toggleClass("active");
        if (toggle == 'closed') {
            $(this).html('More search options');
            toggle = 'open';
            document.getElementById('quicksearch').style.display = 'block';
        }
        else {
            $(this).html('Fewer search options');
            toggle = 'closed';
            document.getElementById('quicksearch').style.display = 'none';
        }
        return false;
    });

    $(".btn-slide-email").click(function(){
        $("#emailpanel").slideToggle("fast");
        $(this).toggleClass("active");
        return false;
    });

    $("#weeks").click(function(){
        if ($(".weeks-nav").is(":hidden")) {
            $(".weeks-nav").slideDown("slow");
        }
        else {
            $(".weeks-nav").hide("fast");
        }
    });

    $("#saved").click(function(){
        if ($(".saved_searches").is(":hidden")) {
            $(".saved_searches").slideDown("slow");
        }
        else {
            $(".saved_searches").hide("fast");
        }
    });

    $("#leadType").click(function(){
        if ($(".lead-nav").is(":hidden")) {
            $(".lead-nav").slideDown("slow");
        }
        else {
            $(".lead-nav").hide("fast");
        }
    });

    function findValue(li){
		var tuid = getUID();
        //if( li == null ) return alert("No match!");

        // if coming from an AJAX call, let's use the CityId as the value
        if (!!li.extra)
            var sValue = li.extra[0];

        // otherwise, let's just display the value in the text box
        else
            var sValue = li.selectValue;

        loadUrl = "?cf=portfolio.postcodeAdd&suppressLayout=true&pc=" + sValue + "&uid=" + tuid;
        $("#pc-tags").html(ajax_load).load(loadUrl);
    }

    function findPCGValue(li){
        //if( li == null ) return alert("No match!");

        // if coming from an AJAX call, let's use the CityId as the value
        if (!!li.extra)
            var sValue = li.extra[0];

        // otherwise, let's just display the value in the text box
        else
            var sValue = li.selectValue;

        content = '<a href="javascript:void(0);" class="p-tag" id="pc_' + sValue + '" onClick="removePCG(\'#pc_' + sValue + '\');"><input type="hidden" name="postcodes" value="' + sValue + '" />' + sValue + '</a> ';
        document.pcgform.pcglookup.value = "";
        $("#pcg-tags").append(content);
    }

    function findCityValue(li){
        //if( li == null ) return alert("No match!");
        var sLabel = "";


        // if coming from an AJAX call, let's use the CityId as the value
        if (!!li.extra) {
            var sValue = li.extra[0];
            sLabel = li.selectValue;
        }

        // otherwise, let's just display the value in the text box
        else
            var sValue = li.selectValue;

        document.granularSearchForm.cityLookup.value = sLabel;
        document.granularSearchForm.cityID.value = sValue;
    }

    function findAdminCityValue(li){
        //if( li == null ) return alert("No match!");
        var sLabel = "";


        // if coming from an AJAX call, let's use the CityId as the value
        if (!!li.extra) {
            var sValue = li.extra[0];
            sLabel = li.selectValue;
        }

        // otherwise, let's just display the value in the text box
        else
            var sValue = li.selectValue;

        try {
            if (sLabel == 'London') {
                document.getElementById('County').value = 'UK - Greater London';
            }
        }
        catch (e) {
            alert(e);
        }

        document.projectForm.city1.value = sLabel;

    }

    function findCompanyValue(li){
        //if( li == null ) return alert("No match!");
        var sLabel = "";

        // if coming from an AJAX call, let's use the CityId as the value
        if (!!li.extra) {
            var sValue = li.extra[0];
            sLabel = li.selectValue;
        }

        // otherwise, let's just display the value in the text box
        else
            var sValue = li.selectValue;

        content = "<br /> " + sLabel;
        c_val = document.linkStoryForm.companyID.value;
        c_val = c_val + "," + sValue;
        document.linkStoryForm.companyID.value = c_val;
        document.linkStoryForm.companyLookup.value = "";
        $("#companyLookupField").append(content);
    }

    function selectItem(li){
        findValue(li);
    }

    function selectPCGItem(li){
        findPCGValue(li);
    }

    function selectCityItem(li){
        findCityValue(li);
    }

    function selectAdminCityItem(li){
        findAdminCityValue(li);
    }

    function selectCompanyItem(li){
        findCompanyValue(li);
    }

    function formatItem(row){
        return row[0];
    }

    function formatCityItem(row){
        return row[0];
    }

    function formatAdminCityItem(row){
        return row[0];
    }

    function formatCompanyItem(row){
        return row[0];
    }

    function lookupAjax(){
        var oSuggest = $("#pc-lookup")[0].autocompleter;
        oSuggest.findValue();
        return false;
    }

    $("#pc-lookup").autocomplete("index.cfm", {
        delay: 10,
        minChars: 1,
        matchSubset: 1,
        matchContains: 1,
        lineSeparator: ',',
        cacheLength: 10,
        onItemSelect: selectItem,
        onFindValue: findValue,
        formatItem: formatItem,
        autoFill: true,
        extraParams: {
            cf: 'portfolio.pcLookup',
            suppressLayout: 'true',
            ajax: 'true',
			uid: getUID()
        }

    });

    $("#pcg-lookup").autocomplete("index.cfm", {
        delay: 10,
        minChars: 1,
        matchSubset: 1,
        matchContains: 1,
        lineSeparator: ',',
        cacheLength: 10,
        onItemSelect: selectPCGItem,
        onFindValue: findPCGValue,
        formatItem: formatItem,
        autoFill: true,
		maxItemsToShow: 5,
        extraParams: {
            cf: 'portfolio.pcLookup',
            suppressLayout: 'true',
            ajax: 'true',
			uid: getUID()
        }
    });

    $("#cityLookup").autocomplete("index.cfm", {
        delay: 10,
        minChars: 1,
        matchSubset: 1,
        matchContains: 1,
        lineSeparator: ',',
        cacheLength: 10,
        onItemSelect: selectCityItem,
        onFindValue: findCityValue,
        formatItem: formatCityItem,
        autoFill: true,
        extraParams: {
            cf: 'search.cityLookup',
            suppressLayout: 'true',
            ajax: 'true',
			uid: getUID()
        }
    });

    $("#cityAdminLookup").autocomplete("/admin/addproject.cfm", {
        delay: 10,
        minChars: 1,
        matchSubset: 1,
        matchContains: 1,
        lineSeparator: ',',
        cacheLength: 10,
        onItemSelect: selectAdminCityItem,
        onFindValue: findAdminCityValue,
        formatItem: formatAdminCityItem,
        autoFill: true,
        extraParams: {
            cf: 'search.cityLookup',
            suppressLayout: 'true',
            ajax: 'true',
			uid: getUID()
        }
    });

    $("#companyLookup").autocomplete("/admin/linkstory.cfm", {
        delay: 10,
        minChars: 1,
        matchSubset: 1,
        matchContains: 1,
        lineSeparator: ',',
        cacheLength: 10,
        onItemSelect: selectCompanyItem,
        onFindValue: findCompanyValue,
        formatItem: formatCompanyItem,
        autoFill: true,
        extraParams: {
            cf: 'search.companyLookup',
            suppressLayout: 'true',
            ajax: 'true',
			uid: getUID()
        }
    });

    /* autocomplete end */

    Array.prototype.exists = function(o){
        for (var i = 0; i < this.length; i++)
            if (this[i] === o)
                return true;
        return false;
    }

    /*
     $(function() {
     $(".accordion").tabs(".accordion div.pane", {tabs: 'h2', effect: 'slide', initialIndex: 0});
     $('.accordion h2').click(function() {
     if ($(this).hasClass('open')) return false;
     $(this).next().toggle('medium');
     open.next().toggle('medium');
     open.removeClass('open');
     open = $(this);
     $(this).addClass('open');
     return false;
     }).next().hide();
     $('.accordion h2').eq(0).next().toggle('medium');
     $('.accordion h2').eq(0).addClass('open');
     try{
     open = $('.accordion h2');
     }catch(e){}
     });
     */
    $(function(){
        $("#accordion-right").tabs("#accordion-right div.pane", {
            tabs: 'h2',
            effect: 'slide'
        });
    });

    $(function(){
        $("#accordion-port").tabs("#accordion-port div.pane", {
            tabs: 'h2',
            effect: 'slide'
        });
    });

    try {
        $(function(){
            $('.date-pick').datePicker({
                startDate: '01/01/1998',
                endDate: (new Date()).asString(),
                clickInput: true,
                createButton: false
            });
        });
    }
    catch (e) {
    }

    try {
        $(function(){
            $('.date-pick-movedate').datePicker({
                startDate: '01/01/1998',
                endDate: '01/01/2020',
                clickInput: true,
                createButton: false
            });
        });
    }
    catch (e) {
    }

    try {
        $(function(){
            $('.date-pick-normal').datePicker({
                startDate: '01/01/1998',
                endDate: (new Date()).asString(),
                clickInput: true,
                createButton: false
            });
        });
    }
    catch (e) {
    }

    try {
        $(function(){
            $('.callback_date').datePicker({
                startDate: (new Date()).asString(),
                endDate: '01/01/2030',
                clickInput: true,
                createButton: false
            });
        });
    }
    catch (e) {
    }

    try {
        $(function(){
            $('#reminder-date').datePicker({
                startDate: (new Date()).asString(),
                endDate: '01/01/2030',
                clickInput: true,
                createButton: false,
                verticalPosition: $.dpConst.POS_BOTTOM
            });
        });
    }
    catch (e) {
    }

    try {
        $(function(){
            $('#reminder-time').timepickr();
        });
    }
    catch (e) {
    }

});

function getUID()
{
     var dateObject = new Date();
     var uniqueId =
          dateObject.getFullYear() + '' +
          dateObject.getMonth() + '' +
          dateObject.getDate() + '' +
          dateObject.getTime();

     return uniqueId;
};


