function getQueryVar(variable) { 
	var query = window.location.search.substring(1); 
	var vars = query.split("&"); 
	for (var i=0; i<vars.length; i++) { 
		var pair = vars[i].split("="); 
		if (pair[0] == variable) { 
			return pair[1]; 
		} 
	} 
	return "none";
}
function insertSite(){

	var flashvars = {};
	flashvars.browser = "true";

	var params = {};
	params.salign = "TL";
	params.scale = "noscale";
	params.allowscriptaccess = "always";
	
	var attributes = {};
	attributes.id = "lloydziff";
	attributes.name = "lloydziff";
	
	swfobject.embedSWF("ziff.swf", "flashcontent", "100%", "100%", "9.0.0", "expressInstall.swf", flashvars, params, attributes);
	//swffit is the flash dynmaic resizer
	swffit.fit("lloydziff", 500, 300);
}

var winW;
var winH;

$(document).ready(function(){

	if( navigator.platform.indexOf("android")>=0 ||
		navigator.platform.indexOf("webOS")>=0 ||
		navigator.platform.indexOf("iPhone")>=0 ||
		navigator.platform.indexOf("iPad")>=0 ||
		navigator.platform.indexOf("iPod")>=0 ) {
		$('#gallery').wrapInner('<table><tr></tr></table>').unwrap().unwrap();
		$('.photoWrap').children().unwrap();
		$('.photo').each(function(){
			var thiP = $(this);
			var bigW = parseInt(thiP.find('.bigW').attr('title'));
			var bigH = parseInt(thiP.find('.bigH').attr('title'));
			thiP.children("img").attr('src',thiP.children("img").attr('src').replace('/thumbs','')).animate({ width: bigW, height: bigH },300);
			thiP.children('textarea').width(thiP.width());
		}).wrap('<td></td>');
	}
	else {

		winW = $(window).width();
		winH = $(window).height();
	
		$('body').css('overflow','hidden');
		$('.subNav').hide().find('a.select').closest('.subNav').addClass('select').show();
		$('#galleryWrap').height(winH);
		$(document).keydown(function(e){
			switch (e.keyCode) {
				case 40:    // down
					$('#galleryWrap').scrollTo({ top: '0', left: $('#gallery').width() },0);
					break;
				case 38:    // up
					$('#galleryWrap').scrollTo({ top: '0', left: '0' },0);
					break;
				case 37:    // left
					$('#galleryWrap').scrollTo({ top: '0', left: '-=60' },0);
					break;
				case 39:    // right
					$('#galleryWrap').scrollTo({ top: '0', left: '+=60' },0);
					break;
			}      
		});

		$('#menuBG').height(winH-35);
		$('#menu').height(winH-37);
		$('#menu .toggle').click(function(){
			if ($(this).hasClass('closed')) {
				$(this).removeClass('closed').css('background-position','0px 0px');
				$('#menuBG').animate({ height: (winH-35), minHeight: '500px' },500);
				$('#menu').animate({ height: (winH-37), minHeight: '498px' },500).children('#nav').fadeIn(700);
				$('#menu img.logo').animate({ top: '120px' },500);
			}
			else {
				$(this).addClass('closed').css('background-position','-11px 0px');
				$('#menuBG').animate({ height: '92px', minHeight: '92px' },500);
				$('#menu').animate({ height: '90px', minHeight: '90px' },500).children('#nav').fadeOut(300);
				$('#menu img.logo').animate({ top: '40px' },500);
			}
		});
	
		$('.photo').each(function(){
			if ($(this).parent().children().length == 1) $(this).unwrap();
			$(this).children('p').width($(this).children('img').width());
		})
		.data('thumb', true)
		.click(function() {
			var thiP = $(this);
			var curO = thiP.offset();
			var galW = parseInt($('#gallery').width());
			var parW = parseInt(thiP.parent().width());
			var imgW = parseInt(thiP.find('.imgW').attr('title'));
			var imgH = parseInt(thiP.find('.imgH').attr('title'));
			var bigW = parseInt(thiP.find('.bigW').attr('title'));
			var bigH = parseInt(thiP.find('.bigH').attr('title'));
			if(thiP.data('thumb')){
				var thiH = thiP.height() + 51 - imgH + bigH;
				if ( thiH > winH ) {
					tempBigH = bigH;
					tempBigW = bigW;
					nonImgH = thiH - bigH;
					bigH = parseInt(winH - nonImgH - 25);
					bigW = parseInt(( bigH / tempBigH ) * tempBigW);
					$(this).data('bigs',{ bigW: bigW, bigH: bigH });
				}
			}
			if ( thiP.data('bigs') ) {
				bigW = thiP.data('bigs').bigW
				bigH = thiP.data('bigs').bigH
			}
			var imgD = parseInt(( winW/2 - imgW/2 ) - curO.left);
			var bigD = parseInt(( winW/2 - bigW/2 ) - curO.left);
			if(thiP.data('thumb')){
				thiP.children("img").attr('src',thiP.children("img").attr('src').replace('/thumbs',''));
				if (thiP.parent().hasClass('photoWrap')) {
					thiP.children("img").animate({ width: bigW, height: bigH },300);
					if (thiP.parent().hasClass('unWrapped')) {
						$('#gallery').css({ width: galW + ( bigW - imgW ) });
						curO = thiP.offset();
						bigD = parseInt(( winW/2 - bigW/2 ) - curO.left);
						$('#galleryWrap').scrollTo({ top: '0', left: '-='+bigD },300);
						thiP.parent().addClass('stayOpen').animate({ width: parW + ( bigW - imgW ) },250);
					}
					else {
						$('#gallery').css({ width: galW + bigW + 50 });
						thiP.parent().addClass('unWrapped').css({ width: parW + imgW + 50 });
						curO = thiP.offset();
						bigD = parseInt(( winW/2 - bigW/2 ) - curO.left);
						$('#galleryWrap').scrollTo({ top: '0', left: '-='+bigD },300);
						thiP.parent().animate({ width: parW + bigW + 50 },250);
					}
				}
				else {
					$('#gallery').css({ width: galW + bigW - imgW });
					thiP.children("img").animate({ width: bigW, height: bigH },300);
					$('#galleryWrap').scrollTo({ top: '0', left: '-='+bigD },300);
				}
			}
			else {
				if (thiP.parent().hasClass('unWrapped')) {
					if (thiP.parent().hasClass('stayOpen')) {
						thiP.parent().removeClass('stayOpen');
						thiP.children("img").animate({ width: imgW, height: imgH },300);
						curO = thiP.offset();
						imgD = parseInt(( winW/2 - imgW/2 ) - curO.left);
						$('#galleryWrap').scrollTo({ top: '0', left: '-='+imgD },300);
						thiP.parent().animate({ width: parW - bigW + imgW },350);
						$('#gallery').animate({ width: galW - bigW + imgW },350);
					}
					else {
						thiP.children("img").animate({ width: imgW, height: imgH },300);
						thiP.parent().removeClass('unWrapped');
						thiP.parent().css({ width: parW - 50  });
						curO = thiP.offset();
						imgD = parseInt(( winW/2 - imgW/2 ) - curO.left);
						$('#galleryWrap').scrollTo({ top: '0', left: '-='+imgD },300);
						thiP.parent().animate({ width: parW - bigW - 50 },350);
						$('#gallery').animate({ width: galW - bigW - 50 },350);
					}
				}
				else {
					thiP.children("img").animate({ width: imgW, height: imgH },300);
					$('#galleryWrap').scrollTo({ top: '0', left: '-='+imgD },300);
					$('#gallery').animate({ width: galW - bigW + imgW },350);
				}
			}
			thiP.data('thumb', !thiP.data('thumb'))
	//		$('#readout').html('winW = '+winW+'<br>curO = '+curO.left+'<br>galW = '+galW+'<br>parW = '+parW+'<br>imgW = '+imgW+'<br>imgH = '+imgH+'<br>bigW = '+bigW+'<br>bigH = '+bigH+'<br>imgD = '+imgD+'<br>bigD = '+bigD);
		});

	}

});

$(window).resize(function(){
	winW = $(window).width();
	winH = $(window).height();
	$('#menuBG').height(winH-35);
	$('#menu').height(winH-37);
	$('#galleryWrap').width(winW).height(winH);
});
