if(typeof String.prototype.trim !== 'function') {
	String.prototype.trim = function() {
		return this.replace(/^\s+/g,'').replace(/\s+$/g, ''); 
	}
}

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */ ) {
        "use strict";
        if (this === void 0 || this === null) {
            throw new TypeError();
        }
        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) {
            return -1;
        }
        var n = 0;
        if (arguments.length > 0) {
            n = Number(arguments[1]);
            if (n !== n) { // shortcut for verifying if it's NaN
                n = 0;
            } else if (n !== 0 && n !== Infinity && n !== -Infinity) {
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
        }
        if (n >= len) {
            return -1;
        }
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) {
                return k;
            }
        }
        return -1;
    }
}



var defaultSearchText = '';
var theVal = 'vehicles';
var isHomeWWW= false;

(function( $ ) {

    $( ".ui-autocomplete-input" ).live( "autocompleteopen", function() {
        var autocomplete = $( this ).data( "autocomplete" ),
    menu = autocomplete.menu;

    if ( !autocomplete.options.selectFirst ) {
        return;
    }

    menu.activate( $.Event({ type: "mouseenter" }), menu.element.children().first() );
    });
    
    commingSoon();

}( jQuery ));

function commingSoon(){
    $('#soon').live('click',function(){
        //alert('tost');
        $('#soon_dlg').html('');
        $('#soon_dlg').html('<iframe id="frame" frameborder="no" scroll="no"  vspace="0" hspace="0" src="http://www.'+base_domain+'/analytics/soon" width="630" height="350"></iframe>');

        
        var dialogOptions = {
            'modal' : true,
            'position' : 'center',
            'title' : '',
            'resizable' : false,
            'height' : 400,
            'width' : 640,
            close : function(event,ui){
                $('#soon_dlg').html('');
            }
        };
        
        
        $('#soon_dlg').dialog(dialogOptions);

        $('#soon_dlg').dialog('open');
       
    });
}


function monkeyPatchAutocomplete() {
}


function getUrlInfo() {
	var get = {};
	var hash = {};
	var length=0;
	if (location.search.length>0) {
		var tmp = location.search.split("?")[1].split("&");
		var item;
		length=tmp.length;
		for (var i=0; i<length; i++) {
			item = tmp[i].split("=");
			if (item[0]!='') {
				get[item[0]]=decodeURIComponent(item[1]);
			}
		}
	}
	if (location.hash.length>0) {
		var tmp = location.hash.split("#")[1].split("&");
		var item;
		length=tmp.length;
		for (var i=0; i<length; i++) {
			item = tmp[i].split("=");
			if (item[0]!='') {
				hash[item[0]]=decodeURIComponent(item[1]);
			}
		}
	}
	var args = decodeURIComponent(location.pathname).replace(/(^\/)|(\/$)/g,'').split('/');
	return {
      domain: location.host,
      path: location.pathname,
      query: location.search,
      get: get,
      hash: hash,
      args: args,
      controller: String(args[0] || '').toLowerCase(),
      what: args[1] || '',
      where: args[2] || '',
      section: location.host.replace(base_domain,'').split(".")[0]
	};
}





function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	//console.debug(name+"="+value+expires+"; path=/");
}


function createCrossDomainCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else 
		var expires = "";
	
	var cross_domain = '' ;
	try{
		if( /[^\.]+\..+/.test(base_domain) )
	    	  cross_domain = '; domain= ' + base_domain ;
	}
	catch(e){
		cross_domain = '' ;
	}
	document.cookie = name+"="+value+expires+"; path=/ " + cross_domain ;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function eraseCrossDomainCookie(name) {
	createCrossDomainCookie(name,"",-1);
}


function populate_promos_home(){
    	params = getUrlInfo()
    	$.get('/adunits/'+params.section+(params.query != '' ? params.query + '&' : '?')+'category='+params.args[1]+'&where='+params.args[2], params.get, function(data) {
        
    	var splitString = '<!-- DELIMIT -->';
        var dataArray = data.split(splitString);
        
        for(var i=0; i < dataArray.length; i++){
            if(dataArray[i]){
                var loc = i+1;
                $('#ad_unit_'+loc).html(dataArray[i]); 
                $('#ad_unit_'+loc).css('display','');
            }
        }
    });
};


$(document).ready( function() {
   
    
    // facebook share
    $("a.fb_share").click(function(){
    	window.open($(this).attr("href"), 'sharepopup',
    			    'width=700,height=400,scrollbars=no');
    	return false;
    });
    
    
});


