$(document).ready( function()
{
	ten7.rollover.init();


	$('#t7_search').bind('keypress', function(e) {
	        if(e.keyCode==13){
	                ten7.search.submit();
	        }
	});
	
	
	if ( $('#adopt_results').length) {
		ten7.animals.get();
	}
	
	if ( $('#t7cookie').length) {
		ten7.cookie.set();
	}
	
	if ( $('#t7_recentanimals').length && $.cookie('t7_most_recent')!=null) {
		ten7.recent.print();
	}
	
	
	
});

ten7 = {};

ten7.recent =
{
	print: function()
	{
	
	if ($.cookie('t7_most_recent')==null) {
  		$.cookie('t7_most_recent',  '&nbsp;' , { path: '/', expires: 100 });
	}
	
	if ($.cookie('t7_more_recent')==null) {
  		$.cookie('t7_more_recent',  '&nbsp;' , { path: '/', expires: 100 });
	}
	
	if ($.cookie('t7_recent')==null) {
  		$.cookie('t7_recent',  '&nbsp;' , { path: '/', expires: 100 });
	}
	
	if ($.cookie('t7_less_recent')==null) {
  		$.cookie('t7_less_recent',  '&nbsp;' , { path: '/', expires: 100 });
	}
	
	if ($.cookie('t7_oldest')==null) {
  		$.cookie('t7_oldest',  '&nbsp;' , { path: '/', expires: 100 });
	}

    $('#t7_recentanimals').html( '<p style="font-weight:bold;">You recently viewed...</p>'+$.cookie('t7_most_recent')+
    								$.cookie('t7_more_recent')+
    								$.cookie('t7_recent')+
    								$.cookie('t7_less_recent')+
    								$.cookie('t7_oldest') );
	
	}


}


ten7.cookie =
{
	set: function()
	{
	
		if ( $('#t7cookie').html()!=$.cookie('t7_most_recent') &&
				$('#t7cookie').html()!=$.cookie('t7_more_recent') &&
				$('#t7cookie').html()!=$.cookie('t7_recent') &&
				$('#t7cookie').html()!=$.cookie('t7_less_recent') &&
				$('#t7cookie').html()!=$.cookie('t7_oldest') 
			) {
	
		  		$.cookie('t7_oldest', $.cookie('t7_less_recent') , { path: '/', expires: 100 });
		  		$.cookie('t7_less_recent', $.cookie('t7_recent') , { path: '/', expires: 100 });
		  		$.cookie('t7_recent', $.cookie('t7_more_recent') , { path: '/', expires: 100 });
		  		$.cookie('t7_more_recent', $.cookie('t7_most_recent') , { path: '/', expires: 100 });
		  		$.cookie('t7_most_recent',  $('#t7cookie').html() , { path: '/', expires: 100 });
		}		

	}

}


ten7.rollover =
{
	init: function()
	{
		this.preload();
		
		$(".mo").hover(
			function () { $(this).attr( 'src', ten7.rollover.newimage($(this).attr('src')) ); },
			function () { $(this).attr( 'src', ten7.rollover.oldimage($(this).attr('src')) ); }
		);
		
		
	},
	
	preload: function()
	{
		$(window).bind('load', function() {
			$('.mo').each( function( key, elm ) { $('<img>').attr( 'src', ten7.rollover.newimage( $(this).attr('src') ) ); });
		});
	},
	
	newimage: function( src ) { return src.replace(".gif","-mo.gif"); },
	oldimage: function( src ) { return src.replace("-mo.gif",".gif"); }
	
};


ten7.search =
{
	submit: function()
	{
		window.location = "http://www.animalhumanesociety.org/search/node/" + $('#t7_search').val() ;
	}
}; 

ten7.animals =
{

  load: function()
  {

     $('#adopt_results').html('<div style="width:100%;text-align:center;font-size:14px;font-weight:lighter;color:#613318;">Loading animal data, please wait...</div>'); 
    

    window.location = "http://www.animalhumanesociety.org/adoption?" +
                      "t=" + $('#s_type').val() + "&" +
                      "a=" + $('#s_agegroup').val() + "&" +
                      "l=" + $('#s_location').val() + "&" +
                      "b=" + $('#s_breed').val() + "&" +
                      "g=" + $('#s_gender').val() + "&" ;
  
  },

	get: function( $force )
	{
	
		$('#adopt_results').val('<div style="width:100%;text-align:center;font-size:14px;font-weight:lighter;color:#613318;">Loading available animals...</div>');
	
		if ($('#s_type').val().length) {
			$st=$('#s_type').val();
		} else {
			$st='dog';
		}
	
		if ($('#s_location').val().length) {
			$sl=$('#s_location').val();
		} else {
			$sl='0';
		}				
	
		if ($('#s_gender').val().length) {
			$sg=$('#s_gender').val();
		} else {
			$sg='0';
		}
		
		if ($('#s_page').val().length) {
			$sp=$('#s_page').val();
		} else {
			$sp='1';
		}

		if ($('#s_breed').val().length) {
			$sb=$('#s_breed').val();
		} else {
			$sb='0';
		}
		
		if ( $force ) {
			$sb='0';
			$('#s_breed').val( '0' );
		}

		$('#adopt_results').load("http://www.animalhumanesociety.org/data/animals/" + $st + "/" + $sl + "/" + $sg + "/" + $sp + "/" + $sb + "/" );
		
		ten7.animals.breeds();

		
		
	},
	
	get2: function()
	{
		$pg2=$('#s_page2').val();
		$('#s_page').val( $pg2 );
		ten7.animals.get();
	},
	
	breeds: function()
	{
	
		if ($('#s_type').val().length) {
			$st=$('#s_type').val();
		} else {
			$st='dog';
		}

		if ($('#s_location').val().length) {
			$sl=$('#s_location').val();
		} else {
			$sl='0';
		}				

		if ($('#s_breed').val().length) {
			$sb=$('#s_breed').val();
		} else {
			$sb='0';
		}
		
			
		$('#breeds').load("http://www.animalhumanesociety.org/data/breeds/" + $st + "/" + $sl + "/" + $sb + "/" );

			
	}
	
};




jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};



