//Digging around eh?
//This is for the my work page
//this script is original and copywrited

pic1= new Image(139,32); 
pic1.src="images/viewwebsite.png";
pic1= new Image(120,32); 
pic1.src="images/viewmore.png";

$(function(){
	//write the empty ULs keep it valid!
	$('.discipline-container:eq(0)').prepend('<ul class="mywork-pager margin-include pager-web"></ul>');
	$('.discipline-container:eq(1)').prepend('<ul class="mywork-pager margin-include pager-brand"></ul>');
	$('.discipline-container:eq(2)').prepend('<ul class="mywork-pager margin-include pager-print"></ul>');
});

var webcurslide = 0;
var bracurslide = 0;
var pricurslide = 0;

//array of associations between slides and reviews
var webassoc = new Array(1,2,4,5,6,7,3,8,9,10,0,0,0,11);
var braassoc = new Array();
var priassoc = new Array();


//this swaps the review when the project is changed
function webswapreview(curr,next,opts){	
	rotatereview('web', opts.nextSlide);
	webcurslide = opts.nextSlide;
}
function braswapreview(curr,next,opts){
	rotatereview('bra', opts.nextSlide);
	bracurslide = opts.nextSlide;
}
function priswapreview(curr,next,opts){
	rotatereview('pri', opts.nextSlide);
	pricurslide = opts.nextSlide;
}

//this defines the slide we're on after its slided for the review slide when changing between disciplines
function webafter(curr,next,opts){
	webcurslide = opts.currSlide;
}
function braafter(curr,next,opts){
	bracurslide = opts.currSlide;
}
function priafter(curr,next,opts){
	pricurslide = opts.currSlide;
}


//this actions the data stored by the after functions to set the review slide when discipline is changed
function discswapper(curr,next,opts){
	switch(opts.nextSlide){
	case 1:
		rotatereview('bra', bracurslide);
	break;
	case 2:
		rotatereview('pri', pricurslide);
	break;
	default:
		rotatereview('web', webcurslide);
	break;
	}
}

//this function rotates the review part
function rotatereview(discipline, slideclicked){
	switch(discipline){
	case 'bra':
		var assoc = braassoc[slideclicked];
	break;
	case 'pri':
		var assoc = priassoc[slideclicked];
	break;
	default:
		var assoc = webassoc[slideclicked];
	break;
	}
	if(assoc == undefined){var i = 0;}else{var i = assoc;}	
	$('.review-container').cycle(i);
}


//init all the cycles
$('.work-cycle').cycle({ 
    fx: 'scrollHorz',
    timeout: 0,
	cleartype: 1,
	containerResize: 1,
	before: discswapper
});
$('.discipline-web').cycle({ 
    fx: 'scrollHorz',
	pager: '.pager-web',
	pagerAnchorBuilder: function(idx, slide) {idx=idx+1; return '<li><a href="#">' + idx + '</a></li>';}, 
    timeout: 0,
	cleartype: 1,
	before: webswapreview,
	after: webafter
});
$('.discipline-brand').cycle({ 
    fx: 'scrollHorz',
	pager: '.pager-brand',
	pagerAnchorBuilder: function(idx, slide) {idx=idx+1; return '<li><a href="#">' + idx + '</a></li>';}, 
    timeout: 0,
	cleartype: 1,
	before: braswapreview,
	after: braafter
});
$('.discipline-print').cycle({ 
    fx: 'scrollHorz',
	pager: '.pager-print',
	pagerAnchorBuilder: function(idx, slide) {idx=idx+1; return '<li><a href="#">' + idx + '</a></li>';}, 
    timeout: 0,
	cleartype: 1,
	before: priswapreview,
	after: priafter
});
$('.review-container').cycle({ 
  	fx: 'scrollHorz',
	timeout: 0,
	cleartype: 1,
	containerResize: 0,
	after: testimonialafter,
	startingSlide: 1
});


//on load
$(function(){
	//navigation for the 3 disciplines
	$('.disciplinenav li a').each(function(i){
		$(this).click(function() { 
			$('.work-cycle').cycle(i);
			$('.disciplinenav li a').removeClass('on');
			$(this).addClass('on');
			return false; 
		});
	});
	
	
	//hover function for web images
	$('.weblink').hover(function(){
		$(this).append('<div class="weblink-inner"><img src="images/viewwebsite.png" /></div>');
		$('.weblink-inner').css({opacity:0}).fadeTo('normal', 0.8);	
	}, function(){
		$('.weblink-inner').fadeOut('normal', function(){$(this).remove();});
	}).bind('click', function(){
		window.open($(this).attr('href'));
		return false;
	});
	
	
	//hover function for print images
	$('.printlink').hover(function(){
		$(this).append('<div class="printlink-inner"><img src="images/viewmore.png" /></div>');
		$('.printlink-inner').css({opacity:0}).fadeTo('normal', 0.8);	
	}, function(){
		$('.printlink-inner').fadeOut('normal', function(){$(this).remove();});
	});
	
	
	//interpret hash and direct to appropriate slide
	var page = location.hash;
	var pagesplit=page.split("-");
	switch(pagesplit[0]){
		case "#web":
			$('.work-cycle').cycle(0);
			$('.web-but').addClass('on');
			$('.discipline-web').cycle(pagesplit[1]-1);
		break;
		
		case "#bra":
			$('.work-cycle').cycle(1);
			$('.bra-but').addClass('on');
			$('.discipline-brand').cycle(pagesplit[1]-1);
		break;
		
		case "#pri":
			$('.work-cycle').cycle(2);
			$('.pri-but').addClass('on');
			$('.discipline-print').cycle(pagesplit[1]-1);
		break;
		
		default:
			$('.web-but').addClass('on');
		break;
	};
	
	
	
	/////////////////////////
	///    Begin Skybox    //
	/////////////////////////
	var current = 0;
	var rel = 0;
	
	$('a.skytrigger').bind('click', function(){
		current = 0;
		$('body').append('<div id="fade" class="sky-overlay"></div><div id="single1" class="sky-content"><table align="center"><tr><td width="40" align="left"><a href="#" class="sky-prev"></a></td><td class="skycycle" align="center" valign="middle"><div class="skyloading">Loading...</div></td><td width="40" align="right"><a href="#" class="sky-next"></a></td></tr></table></div>');
		rel = parseInt($(this).attr('rel'));
		var href = $(this).attr('href');
		var hrefexploded = href.split('.');
		
		//get rid of controls when rel=0
		if(rel==0){$('.sky-prev, .sky-next').remove();}
		
		var i = 0;
		$('.skycycle').append('<img src="'+href+'" class="skyboximage" alt="Gallery" />');//write first
		while(i < rel){i++; $('.skycycle').append('<img src="'+hrefexploded[0]+i+'.jpg" class="skyboximage" alt="Gallery" />');}//write the others
		$('.skyboximage').hide();
		$('.sky-overlay').css({'opacity':0.8});
		$('.sky-overlay, .sky-content').fadeIn();
		
		var imageload = new Image();
		imageload.onload = function() {
			$('.skyloading').remove();
			$('.skyboximage:first').attr('src',href);
			$('.skyboximage:first').fadeIn();
			imageload.onload=function(){};
			
			//put these inside the image load func to prevent pressing them before load
			//next button
			$('.sky-next').bind('click',function(){
				$('.skyboximage:eq('+current+')').hide();
				if(current==rel){current=-1;};
				$('.skyboximage:eq('+(current+1)+')').show();
				current++;
				return false;
			});
			
			//previous button
			$('.sky-prev').bind('click',function(){
				$('.skyboximage:eq('+current+')').hide();
				if(current==0){current=(rel+1);};
				$('.skyboximage:eq('+(current-1)+')').show();
				current--;
				return false;
			});
		};
		imageload.src = href;
		
		
		//close the box
		$('.sky-overlay, .skycycle').bind('click', function(){
			$('.sky-overlay, .sky-content').fadeOut(500,function(){$('.sky-overlay, .sky-content').remove();});
			return false;
		});
		return false;
	});
	
});

//resizes the review window to fit the slide
function testimonialafter(curr, next, opts, fwd){
        //get the height of the current slide
        var ht = $(this).height();
		if(ht < 150){ht=150;};
        //set the container's height to that of the current slide
		$('.review-item').css({height: ht});
		$('.review-container').animate({height: ht});
};