

var utils = {
    baseHref : '',
    ieVersion : -1,
    init : function() {

	this.getIEVersion();
	this.getBaseHref();

	$('a.popupLink').bind('click', function(e) {
	    utils.popupWindow($(this).attr('href'), $(this).attr('title').replace(/[^A-z]/ig, ''),'width=600, height=800, scrollbars');
	    e.preventDefault();
	});
    },
    getIEVersion : function () {
	var rv = -1;
	if (navigator.appName === 'Microsoft Internet Explorer') {
	    var ua = navigator.userAgent;
	    var re  = new RegExp("MSIE ([0-9]{1,}[\\.0-9]{0,})");
	    if (re.exec(ua) !== null) {
		rv = parseFloat( RegExp.$1 );
	    }
	}
	this.ieVersion = rv;
    },
    popupWindow : function (url, name, attributes) {
	// Append popup to the URL if it does not already exist
	var queryStringExists = false;
	if(url.match(/\?/)) {
	    queryStringExists = true;
	}

	if(queryStringExists) {
	    // Only append if we haven't already got the parameter popup defined
	    if(!url.match(/(\?|&)popup=/)) {
		url = utils.baseHref + url + '&popup=true';
	    }
	} else {
	    url =  utils.baseHref + url + '?popup=true';
	}

	var newWindow = window.open(url, name.replace(/[^A-z]/ig, ''), attributes);
	newWindow.focus();
    },
    getBaseHref : function () {
	var baseTags = document.getElementsByTagName('base');
	if(baseTags !== undefined && baseTags.length > 0) {
	    this.baseHref =  baseTags[0].href;
	}
    }
};

var photoSetGallery = {
    galleryHeight: 0,
    galleryOpen: true,
    fadeOutDuration : 750,
    fadeInDuration : 750,
    images: [],
    overlayIndex : 0,
    init : function () {
    windowHeight = $(window).height();
    if(navigator.platform.indexOf("iPad") != -1) {
		resizedImageHeight = windowHeight;
	} else if(navigator.platform.indexOf("iPhone") != -1) {
		resizedImageHeight = windowHeight;
	} else {
		resizedImageHeight = windowHeight - 110;
	}
	$moreFromCatLink = $('#more-from-cat-link');
	$galleryThumbsList = $('#gallery-thumbs');
	$galleryTalentElements = $('#talent-name, #talent-website-link');
	$galleryMenu = $('#gallery-top .gallery-right');
	$animatingElements = $('#talent-name, #talent-website-link, #gallery-top .gallery-right, #gallery-thumbs, #category-talents li');
	this.galleryHeight = $galleryThumbsList.height();
	$categoryTalentList = $('#category-talents');
	$categoryTalentList.removeAttr('style');
	$('li', $categoryTalentList).css({opacity: 0});
	if(photoSetImages != '') {
	    $(photoSetImages).each(function() {
		$(this.talentImageArray).each(function(i) {
		    photoSetGallery.images.push(this);
		});
	    });
	    $(photoSetGallery.images).each(function(i) {
		ratioMed = Number(this.sourceWidth) / Number(this.sourceHeight);
		photoSetGallery.images[i].heightMed = 399;
		photoSetGallery.images[i].widthMed = parseInt(ratioMed * photoSetGallery.images[i].heightMed, null);
		photoSetGallery.images[i].imageUrlMed = utils.baseHref + 'admin-resources/image-tools.php?w='+photoSetGallery.images[i].widthMed+'&amp;h='+photoSetGallery.images[i].heightMed+'&amp;c=0&amp;q=98&amp;src='+ this.file;
		ratioFull =  Number(this.sourceHeight) / Number(this.sourceWidth);
		photoSetGallery.images[i].widthFull = 721;
		photoSetGallery.images[i].heightFull = parseInt(ratioFull * photoSetGallery.images[i].widthFull, null);
		photoSetGallery.images[i].imageUrlResized = utils.baseHref + 'admin-resources/image-tools.php?h='+resizedImageHeight+'&amp;c=0&amp;q=98&amp;src='+ this.file;
		photoSetGallery.images[i].imageUrlFull = utils.baseHref + 'admin-resources/image-tools.php?w='+photoSetGallery.images[i].widthFull+'&amp;h='+photoSetGallery.images[i].heightFull+'&amp;c=0&amp;q=98&amp;src='+ this.file;
	    });
	}
	if($('ul', $galleryMenu).length < 1) {
	    photoSetGallery.toggleMoreTalent();
	}
	this.enableControls();
    },
    disableControls : function () {
	$moreFromCatLink.unbind('click').click(function(e) {e.preventDefault();});
//	$('a', $galleryThumbsList).unbind('click').click(function(e) {e.preventDefault();});
    },
    enableControls : function () {
		$moreFromCatLink.unbind('click').click(function(e) {
		    e.preventDefault();
		    photoSetGallery.toggleMoreTalent();
		});
//		$('a', $galleryThumbsList).unbind('click').click(function(e) {
//			e.preventDefault();
//			console.log('boom');
//			photoSetGallery.initOverlay($('a', $galleryThumbsList).index($(this)));}
//		);
	},
    toggleMoreTalent : function () {
	this.disableControls();
	$animatingElements.stop(true, true);
	if(photoSetGallery.galleryOpen === false) {
	    photoSetGallery.hideMoreTalent();
	} else {
	    photoSetGallery.showMoreTalent();
	}
    },
    showMoreTalent : function() {
	$moreFromCatLink.addClass('active');
	$galleryThumbsList.animate({opacity: 0, height: 10}, {
	    duration: photoSetGallery.fadeOutDuration,
	    easing: 'easeOutExpo',
	    queue: false,
	    complete: function () {
		$galleryThumbsList.addClass('hidden');
		photoSetGallery.galleryOpen = false;
		photoSetGallery.enableControls();
	    }
	});
	$galleryMenu.animate({opacity: 0}, {
	    duration: photoSetGallery.fadeOutDuration,
	    easing: 'easeOutExpo',
	    queue: false
	});
	$galleryTalentElements.animate({opacity: 0}, {
	    duration: photoSetGallery.fadeOutDuration,
	    easing: 'easeOutExpo',
	    queue: false
	});
	setTimeout(function() {
	    photoSetGallery.animateTalentList(0, ($("li", $categoryTalentList).length - 1));
	}, 200);
    },
    hideMoreTalent : function() {
	$moreFromCatLink.removeClass('active');
	$galleryThumbsList.removeClass('hidden').animate({opacity: 1, height: photoSetGallery.galleryHeight}, {
	    duration: photoSetGallery.fadeInDuration,
	    easing: 'easeOutExpo',
	    queue: false,
	    complete: function () {
		photoSetGallery.galleryOpen = true;
	    }
	});
	$galleryMenu.animate({opacity: 1}, {
	    duration: photoSetGallery.fadeInDuration,
	    easing: 'easeOutExpo',
	    queue: false
	});
	$('li', $categoryTalentList).animate({opacity: 0}, {
	    duration: 250,
	    easing: 'easeOutExpo',
	    queue: false,
	    complete: function () {
		$galleryTalentElements.animate({opacity: 1}, {
		    duration: photoSetGallery.fadeInDuration,
		    easing: 'easeOutExpo',
		    queue: false
		});
		photoSetGallery.enableControls();
	    }
	});
    },
    animateTalentList : function (index, count) {
	$('li:eq('+index+')', $categoryTalentList).animate({opacity: 1}, {
	    duration: 750,
	    queue: false
	});
	setTimeout(function () {
	    photoSetGallery.animateTalentList(index + 1, count);
	}, 20);
    },
    initOverlay : function (index) {
	this.disableControls();
	visIndex = index+1;
	this.overlayIndex = index;
	$('body').prepend('<div id="overlay"><div id="overlay-container"><div id="overlay-bg">&nbsp;</div><div id="overlay-gallery"><div id="overlay-gallery-left"><div id="overlay-image-controls"><div id="image-controls-top"><span id="image-index-current">&nbsp;</span><span>/</span><span id="overlay-image-total"></span></span><span id="image-navigation"><a href="#" id="prev-link">&ndash;</a><a href="#" id="next-link">+</a></span></div><h5 id="image-title">&nbsp;</h5></div></div><div id="overlay-gallery-right"><a href="#" id="overlay-close">Close</a><div id="overlay-images-fs" class="loading"></div></div></div></div></div>');

	$('#overlay-bg, #overlay-close').click(function(e) {
	    e.preventDefault();
	    $('#overlay').remove();
	    photoSetGallery.enableControls();
	});

	$('#overlay-image-total').html(photoSetGallery.images.length);
	$imageLargelist = $('#overlay-gallery-image-list');
	$imageControls =  $('#overlay-image-controls');
	$fsContainer = $('#overlay-images-fs');
	$fsButton = $('#overlay-fullscreen');
	$overlayBg = $('#overlay-bg');
		
	this.loadFullScreenImages();
    },
    updateGalleryDisplay : function () {
	$('#image-title', $imageControls).html(photoSetGallery.images[photoSetGallery.overlayIndex].title);
	$('#image-index-current',  $imageControls).html(photoSetGallery.overlayIndex+1);
    },
    disableGalleryControls : function() {
	$('a',  $imageControls).unbind('click').click(function(e) {e.preventDefault();});
    },
    enableGalleryControls : function () {
	$fsButton.click(function(e) {e.preventDefault();photoSetGallery.enableFullScreenImage();});
	$('a',  $imageControls).click(function(e) {
	    e.preventDefault();
	    photoSetGallery.disableGalleryControls();
	    if($(this).attr('id') === 'prev-link') {
		if((photoSetGallery.overlayIndex-1) === -1) {
		    goToIndex = (photoSetGallery.images.length - 1);
		} else {
		    goToIndex = photoSetGallery.overlayIndex-1;
		}
	    } else if($(this).attr('id') === 'next-link') {
		if(photoSetGallery.overlayIndex+1 >= photoSetGallery.images.length) {
		    goToIndex = 0;
		} else {
		    goToIndex = photoSetGallery.overlayIndex+1;
		}
	    }
	    photoSetGallery.showFullImage(goToIndex);
	});
    },
    loadFullScreenImages : function() {
	photoSetGallery.disableGalleryControls();
	$(photoSetGallery.images).each(function(x) {
		windowHeight = $(window).height();
		if(navigator.platform.indexOf("iPad") != -1) {
			resizedImageHeight = windowHeight;
		} else if(navigator.platform.indexOf("iPhone") != -1) {
			resizedImageHeight = windowHeight;
		} else {
			resizedImageHeight = windowHeight - 110;
		}
		//console.log(resizedImageHeight);
	    if(photoSetGallery.images[x].heightFull > resizedImageHeight) {
	    	$fsContainer.append('<span class="image-gallery-image-large" style="width: '+photoSetGallery.images[x].widthFull+'px; height:'+photoSetGallery.images[x].heightFull+'px; background-image: url(\''+photoSetGallery.images[x].imageUrlResized+'\');">&nbsp;</span>');
	    	//$fsContainer.append('<span class="image-gallery-image-large" style="width: '+photoSetGallery.images[x].widthFull+'px; height:'+photoSetGallery.images[x].heightFull+'px; background-image: url(\''+utils.baseHref+'admin-resources/image-tools.php?h='+resizedImageHeight+'&amp;c=0&amp;q=98&amp;src='+ this.file+'\');">&nbsp;</span>');
	    } else {
		    $fsContainer.append('<span class="image-gallery-image-large" style="width: '+photoSetGallery.images[x].widthFull+'px; height:'+photoSetGallery.images[x].heightFull+'px; background-image: url(\''+photoSetGallery.images[x].imageUrlFull+'\');">&nbsp;</span>');
	    	
	    }
	    if(x === (photoSetGallery.images.length-1)) {
		$('.image-gallery-image-large', $fsContainer).css({zIndex: 25, opacity: 0});
		photoSetGallery.updateGalleryDisplay();
		photoSetGallery.disableGalleryControls(); 
		pageHeight = $(document).height();
		//console.log(windowHeight);
		$overlayBg.animate({height : pageHeight}, {
		//$overlayBg.animate({height : (photoSetGallery.images[photoSetGallery.overlayIndex].heightFull + 100)}, {
		    duration: 1500,
		    easing: 'easeOutExpo',
		    queue: false,
		    complete: function() {
			$('.image-gallery-image-large:eq('+photoSetGallery.overlayIndex+')', $fsContainer).animate({opacity: 1}, {
			    duration: 1000,
			    easing: 'easeOutExpo',
			    queue: false,
			    complete: function() {
				photoSetGallery.enableGalleryControls();
				$fsContainer.removeClass('loading');
				$('.image-gallery-image-large:eq('+photoSetGallery.overlayIndex+')', $fsContainer).css({zIndex : 25});
			    }
			});
		    }
		});
	    }
	});
    },
    showFullImage : function(index) {
	currentIndex = photoSetGallery.overlayIndex;
	photoSetGallery.overlayIndex = index;
	heightDiff = (photoSetGallery.images[index].heightFull + 100) - $overlayBg.height();
	if(heightDiff < 0) {
	    heightDiff = heightDiff * -1;
	}	
	
	photoSetGallery.updateGalleryDisplay();
	$('.image-gallery-image-large:eq('+index+')', $fsContainer).stop(true, true).css({opacity: 0, zIndex: 30});
	$('.image-gallery-image-large:eq('+currentIndex+')', $fsContainer).stop(true, true).css({opacity: 1, zIndex: 35});
	$('.image-gallery-image-large:eq('+currentIndex+')', $fsContainer).animate({opacity: 0}, {
	    duration: 250,
	    easing: 'easeOutExpo',
	    queue: true,
	    complete: function () {
		photoSetGallery.enableGalleryControls();
		pageHeight = $(document).height();
		//$overlayBg.animate({height : (photoSetGallery.images[index].heightFull + 100)}, {
		$overlayBg.animate({height : pageHeight}, {
		    //duration: heightDiff,
		    duration: 0,
		    easing: 'easeOutExpo',
		    queue: true,
		    complete: function () {
			$('.image-gallery-image-large:eq('+index+')', $fsContainer).animate({opacity: 1}, {
			    duration: 750,
			    easing: 'easeOutExpo',
			    queue: true,
			    complete: function () {
				$(this).css({zIndex : 25});
			    }
			});
		    }
		});
	    }
	});
    }
};


var talentMenu = {
    init : function () {
	$talentMenuList = $('#talent-menu ul');
	$talentCategoryList = $('#talent-category-menu ul');

	$('a', $talentMenuList).mouseenter(function() {
	    talentIdArray = $(this).attr('class').split(' ');
	    targets = '#' + talentIdArray.join(', #');
	    talentMenu.highlight($(this), targets);
	});
	$('a', $talentCategoryList).mouseenter(function() {
	    talentIdArray = $(this).attr('class').split(' ');
	    targets = '#' + talentIdArray.join(', #');
	    talentMenu.highlight($(this), targets);
	});
	//.click(function(e) {e.preventDefault();});
    },
    highlight : function ($subject, targets) {
	$(targets).addClass('active');
	$subject.parent().unbind('mouseleave');
	$subject.parent().mouseleave(function() {$(targets).removeClass('active');});
    }
};

$(document).ready(function() {

    utils.init();
    if($('#talent-menu-wrapper').length) {
	talentMenu.init();
    }

    $("#gallery-thumbs li a").click(function() {
    	var that = this;
    	
	    $.scrollTo(0, 500 , {easing:'easeOutExpo', 'onAfter':function() {
	    	photoSetGallery.initOverlay($('a', $galleryThumbsList).index($(that)));
	    }});
    	
	    return false;
	    	
	    	
	});
    
    $("#gallery-thumbs").delegate('li a', 'mouseenter', function(){
    	$(this).fadeTo("fast", .7);
    }).delegate('li a', 'mouseout', function(){
    	$(this).fadeTo("fast", 1);
    });
    
    if($('#gallery-main').length) {
	photoSetGallery.init();
    }
});


//TODO: Remove Easing equations not needed

jQuery.easing.jswing = jQuery.easing.swing;

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) { 
		    return c/2*t*t + b;
		} else {
		    return -c/2 * ((--t)*(t-2) - 1) + b;
		}
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) {
		    return c/2*t*t*t*t + b;
		} else {
		    return -c/2 * ((t-=2)*t*t*t - 2) + b;
		}
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t===0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t===d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t===0) {return b;} else
		if (t===d) {return b+c;} else
		if ((t/=d/2) < 1) {return c/2 * Math.pow(2, 10 * (t - 1)) + b;} else {
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;}
	}
});
