$(document).ready(function(){
   
   $('a.ajax_works_thumb').click(function(){
   
		var COOKIE_NAME = 'browser';
	    
	    $.cookie(COOKIE_NAME, $(this).attr('href'), { path: '/', expires: 10 });
        
        var height = $('div#works_main').height();
        $('div#works_main').css('height', height);
        
        var href = $(this).attr('href').split('/');
        $('div#works_main').load('/ajax/works', {url_title:href[5],url_title_artist:href[3]}, function(){$('div#works_main').removeAttr('style'); alter_embeds();});
        return false;
   });
   
   var COOKIE_NAME = 'browser';
   if($.cookie(COOKIE_NAME)) {
	   var lastpath = 		$.cookie(COOKIE_NAME).split('/');
	   var currentpath = 	window.location.href.split('/');

		// is this another works page for the same artist loading? If so use the cookie to load the last viewed image.
	   if(currentpath[3] == 'artists' && currentpath[4] == 'works' && lastpath[3] == currentpath[5]) {
	   		$('div#works_main').hide();
	   		var href = lastpath;
	        $('div#works_main').load('/ajax/works', {url_title:href[5], url_title_artist:href[3]}, function(){$('div#works_main').removeAttr('style'); alter_embeds();});
	   }
	}

   $('div#shows_single_image ul li a').click(function(){
   
        var href = $(this).attr('href').split('/');
        
        if(typeof href[4] != 'undefined')
        {
            $('div#shows_single_image div').load('/ajax/exhibitions/'+href[4], function(){alter_embeds();});
        }else{
            $('div#shows_single_image div').load('/ajax/exhibitions/'+href[3],function(){alter_embeds();});
        }
        
        return false;
   
   });
   
   function paginate() {
       $('p.prevnext a').click(function(){
           
            $('img#loader').show();
            
            //$('p.prevnext').after('<img src="/images/ajax-loader.gif" />');
            var div_height = $('div#view_main').height();
            $('div#holder').css({'height':div_height});
           
            var href = $(this).attr('href').split('/');
            
            $('div#view_main img').hide();
            
            if(href[1] == 'shows') {
            	$('div#holder').load('/ajax/exhibitions_view/'+href[3], function(){$('div#holder').removeAttr('style'); paginate(); $('img#loader').hide(); alter_embeds();});
            } else {
            	$('div#holder').load('/ajax/works_view/'+href[3], function(){$('div#holder').removeAttr('style'); paginate(); $('img#loader').hide(); alter_embeds();});
            }
            
            return false;
            
       });
   }
   
   paginate();
   
   
   $('div.other_works ul').hide();

   // hover over the heading
   $('div.other_works h4').hover(
        function(){
            $(this).next().slideDown('slow');
        },
        
        function(){
            $('div.other_works ul').hover(function(){}, function(){   $('div.other_works ul').slideUp('slow'); });
        }); 
        
    
    function alter_embeds() {
        var div_width   = $('embed').parent().width();
        var emb_width   = $('embed').width();
        var emb_height  = $('embed').height();
        
        var emb_height_no_bar = emb_height - 30;
        var ratio = emb_height_no_bar / emb_width;
        
        var new_height = Math.ceil(div_width * ratio) + 30;
       
        $('embed').css({width:div_width, height:new_height});
    }
    
    alter_embeds();
    
      // NEWSLETTER TEXT INPUT
   // set the default text
	$('#newsletter_email').val($('#newsletter_email').attr('title'));
	
	// clear it out on focus
	$('#newsletter_email').focus(function(){
		var value = $(this).attr('value');
		var title = $(this).attr('title');
		if (value == title) {
			$(this).val("");	
		};
		$(this).addClass("selected");
	});
	// if it's blank then reset back the title attribute
	$('#newsletter_email').blur(function(){
		$(this).removeClass("selected");
		if(!$(this).val().length){
			$('#newsletter_email').val($('#newsletter_email').attr('title'));
		}
	});
	
	
	// HIDES EXHIBITIONS TITLES WITH NO RESULTS 
	
	$('ul.shows_archive_list li.no_results').hide();
	$('ul.shows_archive_list li.no_results').parent().hide();
	$('ul.shows_archive_list li.no_results').parent().prev().hide();
   

});