$(document).ready(function(){
	$("#search_box").focus(function(){
		if(this.value == "Search") {
			this.value = "";
		}
	});
	$("#search_box").blur(function(){
		if(this.value == "") {
			this.value = "Search";
		}
	});
	
	//Menu Hover effect					   
	$('li.top_level').hover(function() { 		
			$('ul', this).css('display', 'block'); 
			$(this).addClass('hover');
		}, function() { 
			$('ul', this).css('display', 'none'); 
			$(this).removeClass('hover');
	});	
	
	$('#ss_sponsorslide').cycle({
		fx:     'fade',
		timeout: 6000
		
	});

	$(".fancybox").fancybox();
	
	 //Events Accordion
	 $(".big_description").hide();
	$(".more_description").toggle(function(){
		$(this).parents("div.description_text").children(".big_description").slideDown();
		$(this).text("Less");
	}, function() {
		$(this).parents("div.description_text").children(".big_description").slideUp();
		$(this).text("Read More");
	});
	 
	 $('#date_picker').datepicker({
		 dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S'],
		 altField: "#eventfinder_date",
		 altFormat: "m-d",
		 onSelect: function(dateText, inst) { 
		 	$('#event_finder').submit();
		 }		
		 });
		 
	//Event Finder
	//$('form#event_finder').jqTransform({imgPath:'/common_images/jqtransformplugin_img/'});
	
	//Random Background image
	checkCookie();
			
	//Borough Map rollover for Venues page
	borough_map_rollover();	

});


function borough_map_rollover()
{
	$(".parks_list li").mouseover(function(){
	var regionMap = 'p#rollovers #'+$(this).attr('id');				
		$(regionMap).css('display', 'inline');	
		// Check if a click event has occured and only change the Region hover state accordingly	
	}).mouseout(function(){
		var regionMap = 'p#rollovers #'+$(this).attr('id');	
		// Check if a click event has occured and only change the Region hover state accordingly
		//if (! $(regionMap).hasClass('selected')) {
			$(regionMap).css('display', 'none');
		//}
	
	});
};

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function checkCookie()
{
	//check to see if the imgNum cookie exists
	if (getCookie('imgNum')!=null && getCookie('imgNum') != ""){
		imgNum=getCookie('imgNum');
		$('body').css({'background-image':'url(/common_images/background_imgs/background_'+imgNum+'.jpg)'});
	}
	//if no imgNum cookie, check to make sure cookies are enable in the browser
	else {
		setCookie('test','0',1);
		//if cookies are enabled, generate a random number
		if (getCookie('test')!=null && getCookie('test') != ""){
			setCookie('test',0,-1);
			var rndNum = 0
			while (rndNum == 0)
			{
				var rndNum = Math.ceil(Math.random() * 5);
			}
		setCookie('imgNum',rndNum,1);		/*setBgImgs(rndNum)*/
		
		$('body').css({'background-image':'url(/common_images/background_imgs/background_'+rndNum+'.jpg)'});
		}
		//if cookies are not enabled set to the 1st image
		else {			
			$('body').css({'background-image':'url(/common_images/background_imgs/background_1.jpg)'});
		}
	}
}

//For Google Analytics to track downloads
function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    }
    else {
        window.onload = function(){
            oldonload();
            func();
        };
    }
};

addLoadEvent(function(){
    var hrefs = document.getElementsByTagName("a");
    var link_path = "";
    for (var l = 0; l < hrefs.length; l++) {
        try {
            var link_path = hrefs[l].pathname;
            if (location.host == hrefs[l].hostname) {
                if (link_path.match(/\.(doc|pdf|xls|ppt|txt|vsd|vxd|wma|mov|avi|wmv|mp3)$/)) {
                    addtrackerlistener(hrefs[l]);
                }
            }
        }
        catch (err) {
        }
    }


    function addtrackerlistener(obj){
        if (obj.addEventListener) {
            obj.addEventListener('click', trackfiles, true);
        }
        else
            if (obj.attachEvent) {
                obj.attachEvent("on" + 'click', trackfiles);
            }
    }

    function trackfiles(array_element){
        file_path = ((array_element.srcElement) ? "/" + array_element.srcElement.pathname : this.pathname);
        urchinTracker(file_path);
    }
});

