﻿var Search = {};
/**
 *  Search Class
 *  
 *  20100602    SCP     REV: search is not performed with the reserved keyword
 *  
 */
(function(s) {
    var Search = {
        _map : null,
        _infobox : null,
        _thumbnail : null,
        _timeline : null,
        
        /**
         * Search term within the search box
         * 
         * @type String
         */
        _term : '',
        
        /**
         * flag to show whether the submition of the form is enabled
         * 
         * @type Boolean
         * @default false
         */
        _submitEnabled : false,
        
        /**
         * Binds the search box to the event listeners
         * 
         * @public
         * @param {void}
         * @return {void}
         */
        bind : function() 
        {
            // when this event is fired the search term is cleared
            // from the query string and is not added to the
            // database queries
            $(document).bind('clear-search', function(e, data) {
                Search.clearSearch();
            });
            
            $('.searchSubmit').unbind().click(Search.performSearch);
            
            $('input.search').click(function() {
                if ($(this).css('color') != '#000') {
                	// keep the current reserved keyword for the
                	// mouseout event
                    Search._term = $(this).val();
                    // remove the keyword from the box
                    // remove the rel element
                    // change the color of the box from gray to black
                    $(this).val('')
                           .attr('rel', '')
                           .css({color:"#000"});
                }
            });
            
            // define onblur event of the box
            $('input.search').blur(function() {
            	// if the word is empty return
            	// the prevoius state
                if ($(this).val() == '') {
                	// black to gray color of the box
                	// reserved word in the box and
                	// rel attribute == to 'search'
                    $(this).css({color:'#ccc'})
                           .val(Search._term)
                           .attr('rel', 'search');
                }
            });
            
            // define a keypress event to the box
            $('input.search').keypress(function(e) { 
                if (e.which == 13) {
                    Search.performSearch(); 
                    return false;
                } 
            });
            
            //Advanced search
            $('.clickToSearch').click(function() {
                document.frmSearch.submit();
            });
            
            $('span.clickToSearch').keypress(function(e) { 
                if (e.which == 13) {
                    document.frmSearch.submit();
                } 
            });
            
            var dpObject = { 
                yearRange       : '1895:2100',
                dateFormat      : 'yy/mm/dd',
                showOn          : "button",
                buttonImage     : "/images/cal.gif", 
                buttonImageOnly : true,
				closeText		: 'close[x]',
				showButtonPanel: true
            };
            
            $('#dtpUFD').datepicker(dpObject);
            $('#dtpUTD').datepicker(dpObject);
            $('#dtpAFD').datepicker(dpObject);
            $('#dtpATD').datepicker(dpObject);
            
            $('#ui-datepicker-div').css('z-index', '200000');
        },
        
        /**
         * enables the search form to submit and redirect the page to home
         * used in pages different than home
         * 
         * @public
         * @param {Boolean} enabled
         */
        enableSubmition : function(enabled) 
        {
            if (typeof enabled != 'undefined') {
                Search._submitEnabled = enabled;
            }
        },
        
        /**
         * performs the search 
         * 
         * @return {Boolean} faslse - prevents the default action
         */
        performSearch: function() 
        {
        	// get the rel element of the box and
            // if its value is equal to 'search' proceed
            // otherwise stop here
            if ('search' == $('input.search').attr('rel')) {
                return false;
            }
            
        	// change the button from search to clear search icon
            $('div.select').addClass('search-clear');
            // unbind the click event for search and
            // bind the clear search event
            $('.searchSubmit').unbind().click(function() {
                $(document).trigger('clear-search');
            });
            
            // get the keyword from input field
            var keyword = $('input.search').val();
            
            // if keyword is not undefined
            if (typeof keyword !== 'undefined' && keyword != '') {
                // set it to queryString object
                BaseController.setQueryString({
                    keywords : keyword.replace(/ /g, '+'),
                    category : 0,
                    fd       : '1895-03-22',
                    td       : '2100-12-31'
                });
                
                // if the page is setup to be refreshed after form submit
                // redirect to home with this keyword
                if (Search._submitEnabled) {
                    location.href = '/#/keywords=' + keyword + '/';
                    return false;
                }
    
                $(document).trigger('search-changed');
                BaseController.rewriteUrl();
                return false;
            }
            
            $('#searchHolder').css({
                background : 'transparent url(../images/master.png) no-repeat scroll right -384px'
            });
            return false;
        },
        
        /**
         * Clears the search term from the query string object and
         * refreshes the query to the objects
         * 
         * @return {Boolean}
         */
        clearSearch : function() 
        {
        	// change the search box color settings and
        	// return the previousely searched term
            $('input.search').css({color: '#ccc'})
                             .html(Search._term);

            // clear the keywords if there's something to clear ;)
            if (BaseController.getQueryString().keywords != '') {
                BaseController.setQueryString({keywords:''});
            } else { 
                return false;
            }
            
            // rewrites the url
            BaseController.rewriteUrl();
            
            // removes the "x" and put the right icon
            $('div.select').removeClass('search-clear');
            
            // unbind the clear search from the button
            // and bind new click - to perform search
            $('.searchSubmit').unbind().click(Search.performSearch);
            
            // refresh the search by firing an 'search-changed' event 
            $(document).trigger('search-changed');
            
            // prevent the default action
            return false;
        }
    };
    
    // export to public
    s.bind            = Search.bind;
    s.enableSubmition = Search.enableSubmition;
})(Search);


$(document).ready(function(){
	$("#lostoffers").click(function(){
		$(".content_lostdeals").show();   
		$(".content_registration").hide();  
		$(".content_theway_itgo").hide();   
		$(".actualdeals").hide();   
	});
});
