var FinderController = Class.create({
    initialize: function() {
    	this.lastDrop = { what: null, on: null };
    },

    setCurrency: function() {
        $('currencyWorking').show();
        var options = { parameters: { c: $('currencySelect').getValue() },
                        onSuccess: function(transport) {
					       location.href = $('DefaultLink').href;
					       $('ccLink').prototip.hide();
					    }
                     };

        new Ajax.Request('/finder/setcurrency/',options);
    },

    handleDrop: function(dragElement,dropElement) {
        var dragID = dragElement.id.split('_').pop();
        var critID = 'c'+dropElement.id.substr(dropElement.id.length-1,1);
        var originalDragElement = dragElement.id.split('_').slice(1).join('_');

        f.lastDrop = { what: dragID, on: critID };
        if($(critID).value == '') {
            $(originalDragElement).setStyle({
                display: 'none'
            });
            $(dropElement).setStyle({
                background: '#223c5b',
                border: '2px #c9c1b4 dashed',
                color: '#fff',
                height: '25px',
                padding: '10px 0 0 13px',
                width: '321px'
            });
        } else {
            dragElement.setStyle({
                display: 'none'
            });
           var origElement = $(critID).value;
           $(origElement).setStyle({
                display: 'block',
				background: '#bfb4a6 url(../i/finder-drag-icon.gif) no-repeat 284px 14px',
				border: 'none',
				color: '#50463c',
				height: '27px',
				marginBottom: '3px',
				padding: '12px 0 0 18px',
				width: '320px'
            });
            $(dropElement).setStyle({
                background: '#223c5b',
	            border: '2px #c9c1b4 dashed',
	            color: '#fff',
	            height: '25px',
	            padding: '10px 0 0 13px',
	            width: '321px'
            });
        }
        dropElement.innerHTML = dragElement.innerHTML +
                                '<a href="javascript://" onclick="f.removeFromCrit(\'' +
                                dragID+'\',\''+critID+'\')" class="Remove">Remove</a>';
        $(critID).value = dragID;
    },


    removeFromCrit: function(dragID,critID) {
    	f.lastDrop.what = null;
        $(critID).value = '';
        critID = 'Criteria' + critID.split('c')[1];
        $(critID).setStyle({
            background: '#fffff2',
		    border: '2px #c9c1b4 dashed',
		    color: '#cfcdbe',
		    height: '25px',
		    padding: '10px 0 0 84px',
		    width: '250px'
        });
        $(critID).innerHTML = 'Drop your selection here!';
        this.restoreCharacteristic(dragID);
    },

    restoreCharacteristic: function(dragID) {
        if(f.lastDrop.what != dragID) {
	        $('char_'+dragID).setStyle({
	            display: 'block',
	            background: '#bfb4a6 url(../i/finder-drag-icon.gif) no-repeat 284px 14px',
	            border: 'none',
	            color: '#50463c',
	            height: '27px',
	            marginBottom: '3px',
	            padding: '12px 0 0 18px',
	            width: '320px'
	        });
        }
    },

    toggleActive: function(element) {
        if(element.className == 'CharCat') {
            element.className = 'CharCatActive';
        } else {
            element.className = 'CharCat';
        }
    },

    updateCookie: function(val) {
	    var cookie = g.getCookie('savedtours')
	    if(cookie !== '') {
	        var ids = cookie.split(',');
	        var newids = new Array();
	        var found = false;
	        ids.each(function(s) {
	            if(s == val) {
	                found = true;
	            } else {
	                if(s !== '') {
	                    newids = newids.concat(s);
	                }
	            }
	        });
	        if(!found) {
	            newids = newids.concat(val);
	        }
	        idstr = '';
	        newids.each(function(i) {
	            idstr += i + ',';
	        });
	        g.setCookie('savedtours', idstr, 1);
	    } else {
	        g.setCookie('savedtours', val, 1);
	    }
	},

	datesLoaded: function() {
        // dates
        $$('.Desc').each(function(i) {
            i.href = 'javascript://';
            var dateId = i.id.substr(4);
            var url = '/polar-bear-tours/resources/date/id/'+dateId+'/';
            var title = '<div>Trip Details for:</div>' + i.title.toUpperCase();
            new Tip(i.id, {
                title: title,
                ajax: { url: url },
                closeButton: true,
                showOn: 'click',
                hideOn: { element: 'closeButton', event: 'click'},
                stem: 'leftMiddle',
                hook: { target: 'topRight', tip: 'leftMiddle' },
                offset: { x: 0, y: 5 },
                width: 'auto',
                style: 'fna',
                viewport:true,
                radius: 0

            });
        });
        var url = '/finder/currencies/';
        new Tip('ccLink', {
            title: "Choose Your Desired Currency",
            ajax: { url: url },
            showOn: 'click',
            closeButton: true,
            hideOn: { element: 'closeButton', event: 'click'},
            stem: 'rightMiddle',
            hook: { target: 'topLeft', tip: 'rightMiddle' },
            offset: { x: 0, y: 5 },
            width: 'auto',
            style: 'fna',
            viewport:true,
            radius: 0
        });
        var url = '/finder/help/show/2/';
        new Tip('listHelpLink', {
                title: 'Help',
                ajax: { url: url },
                closeButton: true,
                showOn: 'click',
                hideOn: { element: 'closeButton', event: 'click'},
                stem: 'rightMiddle',
                hook: { target: 'topLeft', tip: 'rightMiddle' },
                offset: { x: 0, y: 5 },
                width: 'auto',
                style: 'fna',
                viewport:true,
                radius: 0
        });
    },

    finderLoaded: function() {
        var url = '/finder/help/show/1/';
        new Tip('finderHelpLink', {
                title: 'Help',
                ajax: { url: url },
                closeButton: true,
                showOn: 'click',
                hideOn: { element: 'closeButton', event: 'click'},
                stem: 'rightMiddle',
                hook: { target: 'topLeft', tip: 'rightMiddle' },
                offset: { x: 0, y: 5 },
                width: 'auto',
                style: 'fna',
                viewport:true,
                radius: 0
        });
    }

});

var f = new FinderController();
