$(function(){

	$(window).load(function(){

		if($('.images_holder').length > 0){
		$('.thumbnail').rollfade();
		$('.thumbnail').each(function(){
			var el = $(this);
			var i = el.find('img');
			var w = i.outerWidth();
			var h = i.outerHeight();
			var ratio = w/h;

			// check width

			var cssoptions = {};
/* 			var thistop = 0; */
/* 			var thisleft = 0; */

			if(112/ratio < 70){
				cssoptions.height = 70;
				cssoptions.width = ratio*70;
				if(ratio*70 > 112){
					cssoptions.width = 112;
					cssoptions.height = 112/ratio;
				}
			} else {
				cssoptions.width = 112;
				cssoptions.height = 112/ratio;
			}
			
			if(cssoptions.height < 70){
				cssoptions['position'] = 'relative';
				cssoptions.top = (70-cssoptions.height)/2;
				cssoptions.top += 'px';
			}

			cssoptions.width += 'px';
			cssoptions.height += 'px';

			i.css(cssoptions);

		});
		}

		if($('#controls').length > 0){
			resizeControls();
		}

	});


	$('.rollover').rollover();

	$('.ourwork-tab:not(.nohover)').each(function(){

		var el = $(this);

		el.append('<div class="thisright"> </div>');
		el.prepend('<div class="thisleft"> </div>');

		el.hover(function(){
			el.css({'background' : '#FFF', color : '#000', 'border-top-width' : 0, 'margin-top' : '2px'});
			el.children('.thisright').css({'background': '#d3d500 url(/images/ourwork-tabright_1.jpg) center 1px no-repeat'});
			el.children('.thisleft').css({'background': '#d3d500 url(/images/ourwork-tableft_1.jpg) center 1px no-repeat'});

		}, function(){

			el.attr('style', '');
			el.children('.thisright, .thisleft').attr('style', '');
		});

	}); // close tabs


	$('#controls a').each(function(){

		var el = $(this);
		var cs = $('.currentslide');
		var r = el.attr('href');

		$('#slides').append('<img src="'+r+'" />');

		var thisid = 'grabber_'+Math.floor(Math.random()*102109310293);

		el.attr('grabber' , thisid);

		el.click(function(){

			$('#controls a:not([grabber='+thisid+'])').removeAttr('style').removeAttr('active');

			el.css('font-weight', 'bold');
			el.attr('active', "true");

			cs.stop().fadeTo(0,1);
			cs.fadeOut(300, function(){
				cs.attr('src', r);
				cs.fadeIn(300, function(){
					resizeControls();
				});
			});
			return false;

		});
	}); // close controls

	$('#controls .up, #controls .down').click(function(){
		var el = $(this);
		var cs = $('.currentslide');
		var active = $('#controls [active=true]');

		// get c
		var c = active.text();

		// get direction
		var direction = el.attr('direction');
		direction = direction-1+1;
		c = c-1+1;
		c = c + direction;

		// get total
		var total = $('#controls a').length;

		if(c <= total && c > 0){
			$('#controls a').each(function(){

				if($(this).text() == c){
					$(this).click();
				}

			});
		} else {
			if(c > total){
				$('#controls a:first').click();
			} else {
				$('#controls a:last').click();
			}
		}

	});



	function resizeControls () {

		var c = $('#controls');
		var cs = $('.currentslide');

		c.css('height', cs.outerHeight()-17+'px');

	}


	// customised flock-icon functions

	$.fn.flockicon = function(options){

		var options = $.extend({
			checkClass : 'clicked',
			fadeTo : 1,
			duration : 500,
			storypanel : '#storypanel'
		}, options);

		this.each(function(){

			var el = $(this);
			var prt = el.parent();
			var sp = $(options.storypanel);

			// the element 'story' attribute references a hidden div with the necessary story html in it.

			var sh = $(el.attr('story'));

			var thisid = 'grabber_'+Math.floor(Math.random()*1020310293);
			el.attr('grabber', thisid);

			el.click(function(){
				$('.clicked').removeClass('clicked').mouseout();
				el.addClass('clicked');
				el.stop().fadeTo(options.duration, options.fadeTo);


				sp.stop(true);

/* 				if(sp.find('.thisright').html() != sh.html()){ */
				sp.queue(function(){
					sp.find('.thisright').hide();
					sp.dequeue();
				});
/* 				} */
				// calculate y axis relative to parent

				var thistop = el.position().top;

			//	thistop += el.position().top+10;

				if(sp.position().top != thistop){
					sp.animate({left : '100%', top : thistop+'px'},500, "easeOutBack");

				} else {
					sp
						.animate({left : '99%'}, 100)
						.animate({left : '101%'}, 200)
						.animate({left : '100%'}, 100);
				}

				// send text data
				sp.queue(function(){
					sp.find('.thisright').html(sh.html()).show();
					sp.dequeue();
				});
				sp.find('.thisleft img').attr('src', el.attr('src').replace('/Flock-', '/Large-Flock-'));
			});
		});
	};

	// build

	$('.flock-icon').rollfade({
		fadeOut : 0.4,
		fadeIn : 0.7
	});

	$('.flock-icon').flockicon();

});

$(document).ready(function(){
  // toggles the slickbox on clicking the noted link  
  $('#slickbox').hide();
  
  $('#slick-toggle').click(function() {
    $('#slickbox').slideToggle(400);
    if ($('#slick-toggle img').attr('src')=='/images/expand.gif') {
        $('#slick-toggle img').attr('src','/images/collapse.gif');
    } else {
        $('#slick-toggle img').attr('src','/images/expand.gif');
    }
    return false;
  });
});
