$(document).ready(function(){
	
	$('.nohide').removeClass('nohide');
	$('form#filterbyartist button').remove();
	
	thumb_pagination();
    add_slider_links();
    swap_main_works_image();
    swap_works_multiple_images();
    slide_next();
    slide_prev();
    display_exhib_images();
    alter_embeds();
    search_text_input();
    scrollbars();
    
    $('p#video_caption a').click(function(){
        
        //$(this).attr('href');
        
        $(this).parent('div#sb-container').remove();
        return false;
        
    });
    
    
    $('ul#exhibition_thumbs li:first').addClass('cur');
    $('ul#news_scroller li:eq(0)').css({top:'0'}).addClass('cur');
    setInterval(rotate, 7000);
    
    
    
});

function rotate( ) {
    var headline_count      = $('ul#news_scroller li').length - 1;
    var current_headline    = $('ul#news_scroller li').index($('ul#news_scroller li.cur'));
    if( current_headline < headline_count) {
        next_headline = current_headline + 1;
    }
    else {
        next_headline = 0;
    }
    var old_headline        = current_headline;
    
    $("ul#news_scroller li.cur").animate({"top": "-42px"}, "slow",function(){
        $(this).removeClass('cur').css('top','42px');
        $('ul#news_scroller li:eq('+next_headline+')').animate({"top": "0px"}, "slow").addClass('cur');
    });
}


function change_parent_url(url)
{
    document.location=url;
}



/**
    * ALTER EMBEDS
    * Deals with videos embeds & sizes etc
    * ----
*/
function alter_embeds() {

    if($('div#works_main span#noflash').length > 0){
                   
    	var dimensions 	= $('div#works_main span#noflash a.flashvars').attr('title').split('|');
    	var ratio 		= dimensions[1] / dimensions[0];

        flashvars = $('div#works_main span#noflash a.flashvars').attr('href');

    	var s1 = new SWFObject("/flash/jwplayer.swf","ply",dimensions[1],dimensions[0],"9");
        s1.addParam("allowfullscreen","true");
        s1.addParam("allowscriptaccess","always");
        s1.addParam('wmode','opaque');
        s1.addParam("flashvars","file="+flashvars+"&skin=/flash/tt-new2-1.swf");
        s1.write("noflash");
    
        var max_width  = $('embed').parent().parent().width();
    	var max_height = $('embed').parent().parent().height();

    	var new_width = max_width;
    	var new_height = Math.ceil(max_width * ratio) + 29;
	
	
        $('embed').attr('height', new_height);
        $('embed').attr('width', new_width);
	        
    }
}



 /**
     * EXHIBITION IMAGES SLIDING - THUMBNAILS
     * Slides the images to the thumbnail you've just clicked
     * ----
 */
function add_slider_links () {

    $('ul#exhibition_thumbs li a').click(function(){
        
        var indexes = $('ul#exhibition_thumbs li a').length;
        var n       = $('ul#exhibition_thumbs li a').index($(this));
        
        $('ul#exhibition_thumbs li.cur').removeClass('cur');
        $(this).parent().addClass('cur');
        
        var pos = n*460;
        
        $('div#exhibitionview_main div').scrollTo({top:'0px', left:pos+'px'}, 800, {axis:'xy' });
        
        scope(n,indexes);
        
        return false;
    
    });    

}



/**
    * EXHIBITION IMAGES SLIDING - NEXT
    * Slides the images to the next one
    * ----
*/
function slide_next() {

    $('div#exhibitionview_main ul  li.next a').click(
        function(){
            
            var indexes = $('ul#exhibition_thumbs li a').length;
            var n       = $('ul#exhibition_thumbs li a').index($('ul#exhibition_thumbs li.cur a'));

            if(n > -1 && (n +1) < indexes){
                $('ul#exhibition_thumbs li.cur').removeClass('cur').next().addClass('cur');
                var pos = (n*460) + 460;

                $('div#exhibitionview_main div').scrollTo({top:'0px', left:pos+'px'}, 800, {axis:'xy' });
                
                 scope(n +1,indexes);

            }
            return false;
        }
    );

}



/**
    * EXHIBITION IMAGES SLIDING - PREV
    * Slides the images to the previous one
    * ----
*/
function slide_prev() {

    $('div#exhibitionview_main ul li.prev a').click(
        function(){
            var indexes = $('ul#exhibition_thumbs li a').length;
            var n       = $('ul#exhibition_thumbs li a').index($('ul#exhibition_thumbs li.cur a'));
            
            if(n > 0){
                
                $('ul#exhibition_thumbs li.cur').removeClass('cur').prev().addClass('cur');
                var pos = (n*460) - 460;
                $('div#exhibitionview_main div').scrollTo({top:'0px', left:pos+'px'}, 800, {axis:'xy' });
            
                scope(n - 1,indexes);
            }
            return false;
        }
    );
    
}



/**
    * MAIN WORKS IMAGES
    * AJAXes the swapping of main images from thumbnails
    * ----
*/
function swap_main_works_image() {

    // swap main works image
    $('ul#works_thumbnails li a').click(
        function() {
            var page = $(this).attr('href');
            
            $('div#works_main').fadeTo(200, 0, function(){
                
                $("div#works_main").load(page+' #works_main >',

                function() {
                    swap_works_multiple_images();
                    alter_embeds();
                    $('div#works_main').fadeTo(300, 1);
                });   
            });
         return false; 
        }
    );

}



/**
    * THUMBNAIL PAGINATION
    * AJAX thumbnail pagination
    * ----
*/
function thumb_pagination () {
    
    $('div#thumb-wrapper div#pagination a').click(
        function(){
            var page = $(this).attr('href');
            $('div#thumb-wrapper div#pagination a.cur').removeClass('cur');
            $(this).addClass('cur');
            $("div#thumb-wrapper").height($("div#thumb-wrapper").height());
            
            $("div#thumb-wrapper ul").fadeTo(200, 0).load(page+' div#thumb-wrapper ul li',
            
            function() {
                swap_main_works_image();
                $("div#thumb-wrapper ul").fadeTo(200, 1);
            });
            
            return false;
        }
    );
    
}



/**
    * WORKS MULITPLE IMAGES
    * Uses AJAX to load in mulitple works images
    * ----
*/
function swap_works_multiple_images() {

    $('li.multiple_images a').click(
        function() {
        
            var page = $(this).attr('href');
             $('li.multiple_images a').removeClass('cur');
             $(this).addClass('cur');
            
            $("div#works_main p:first").fadeTo(200, 0).load(page+' #works_main p:first >',function(){
                $("div#works_main p:first").fadeTo(200, 1)
            });
            
            return false;    
        
        }
    );

}



/**
    * DISPLAY EXHIBITION IMAGES
    * Fades in the main exhibition image
    * ----
*/
function display_exhib_images() {

    $('div#exhibitionview_main ol li img').fadeIn('slow',
        function(){
            $('div#exhibitionview_main ol li img').css({display:'inline'});
        }
    );

}



/**
    * SCOPE
    * Exhibition walkthrough images scrolling - disables "prev" arrow when you're at the first, and disables "next" arrow when you're at the end
    * ----
*/
function scope(n, indexes) {

    if(n == 0) {
        $('div#exhibitionview_main ul  li.prev').addClass('scope'); 
    } else {
            
        $('div#exhibitionview_main ul  li.prev').removeClass('scope');
    }
        
    if(n + 1 == indexes) {
        $('div#exhibitionview_main ul  li.next').addClass('scope');
    } else {
        $('div#exhibitionview_main ul  li.next').removeClass('scope');
    }

}



/**
    * SEARCH TEXT INPUT
    * On focus gets rid of "value" attr, and replaces if off focus
    * ----
*/
function search_text_input() {
    // set the default text
    $('input.keywords').focus(function(){
	    var value = this.value;
		var title = this.title;
		if (value == title) {
			$(this).val("");
		};
	});

	// if it's blank then reset back the title attribute
	$('input.keywords').blur(function(){
		if(!$(this).val().length){
		    $(this).val(this.title);
		}
	});
}



/**
    * SCROLLBARS
    * Default scrollbars get replaced by nice ones
    * ----
*/
function scrollbars() {
    var params = new scrollBarParams();
	$('div#exhibition_pressrelease').jScrollPane( params );
	$('div#artists_bio').jScrollPane( params );
}

function scrollBarParams() {

    //this.scrollbarMargin = '15';
}

function loadinparent(url){
	self.opener.location = url;
}