var fb_opened = 0;
var gbl_html='';
var polaroid_controls=false;
var nyro_block=false;


$(document).ready
(
	
	function()
	{
		
		$('a[rel*=gallerybox]').click(function()
		{
			nyro_block=false;//open as pop up
			this_loc = $(this).attr('href');//add js to prevent gallery incorporating data into ordinary page (as will happen in disabled js browsers)
			setStartPosition(this_loc);
			openfaceBox(this_loc,1,0,false);
			//alert(this_loc)
			return false;
		});
								 
								 
	
		/*$('a.open_nyro_block').click(function()
		{
			this_loc = $(this).attr('href');//add js to prevent gallery incorporating data into ordinary page (as will happen in disabled js browsers)
			setStartPosition(this_loc);
			nyro_block = 'div.polaroid_frame';//open as block
			openfaceBox(this_loc,1,0,'div.polaroid_frame');
			return false;
		});	*/
								 
								 
	
		$('a[rel*=misc_pop]').click(function()
		{
			
			this_loc = $(this).attr('href');//add js to prevent gallery incorporating data into ordinary page (as will happen in disabled js browsers)
			openfaceBox(this_loc,1,1,false);
			return false;
		});
		
		$('a[rel*=next]').click(
		function()
		{
			if($(this).hasClass('nyro_disabled') ){return false;}
			this_loc = $(this).attr('href');
			if($(this).hasClass('open_nyro_block') )
			{
				disableControls();
				setNextEvent(this_loc,'div.polaroid_frame');
			}
			return false;
		});
							   
		$('a[rel*=prev]').click(
		function()
		{
			if($(this).hasClass('nyro_disabled') ){return false;}
			this_loc = $(this).attr('href');
			if($(this).hasClass('open_nyro_block') )
			{
				disableControls();
				setPrevEvent(this_loc,'div.polaroid_frame');
			}
			return false;
		});
							   
		
		
	}
	
	
);

function disableControls()
{
	$('a.open_nyro_block').not('a.abs_disabled').addClass('nyro_disabled').hide();//stop user clicking before nyromodal finished loading
}

function setStartPosition(link_path)
{
	current_gallery = urls.indexOf(link_path);//when user clicks from a gallery image this informs next buttons on opened page what next and previous indexs are
	
	//note IE does not recognise indexOf - this requires the indexOf prototype definition in misc.js
}

/* minWidth:gallery_width,
						  */

function openfaceBox(this_loc,ev_flag,misc,block_type)
{
	rand = Math.random();
	rand= '&amp;rand='+rand;
	this_loc=this_loc+'&amp;js=1'+rand;
	//alert(this_loc)
	if(misc==0)//for team and portfolio
	{
		gal_width = gallery_width;
		gal_height= gallery_height;
	}
	else//for privacy policy and other pop ups
	{
		gal_width = misc_width;
		gal_height= misc_height;
	}
	
	
	$.nyroModalManual({	
						   url: this_loc,
						   width:gal_width,
						   height:gal_height,
							blocker:block_type,
							endFillContent: function(elts, settings) 
							{
								if($('div.p_c_team').length)
								{
									
									var h = elts.contentWrapper.height();
									bodyHeight = $('div.pop_content').css('height').replace(/px/,'')*1;
									scrollHeight=10;
									bodyTopPad = $('div.pop_content').css('padding-top').replace(/px/,'')*1;
									headerHeight = $('div.pop_container div.header').css('height').replace(/px/,'')*1;
									headerTopPad = $('div.pop_container div.header').css('padding-top').replace(/px/,'')*1;
									headerBotPad = $('div.pop_container div.header').css('padding-bottom').replace(/px/,'')*1;
									backGroundTopPad =  $('div.backer').css('padding-top').replace(/px/,'')*1;
									bodyTopPosOffset = bodyTopPad+headerHeight+headerTopPad+headerBotPad+backGroundTopPad;
									bodyVertical = bodyHeight+bodyTopPosOffset+scrollHeight;
									if(bodyVertical > h)
									{
										manualAdjust = 0;
										difference = bodyVertical-h;
										adjustedBody = bodyHeight-difference-manualAdjust;
										//adjustedBody = 254;
										$('div.pop_content').css('height',adjustedBody);
										//alert('Adjustedbody'+adjustedBody+'wrapper height: '+h+' bodyTopPad '+bodyTopPad+' Top Offset: '+ bodyTopPosOffset+' Vertical: '+bodyVertical);
									
									}
									setControls(block_type);
									
								}
								else if($('div.p_c_misc').length)
								{
									
									var h = elts.contentWrapper.height();
									bodyHeight = $('div.pop_content').css('height').replace(/px/,'')*1;
									scrollHeight=10;
									bodyTopPad = $('div.pop_content').css('padding-top').replace(/px/,'')*1;
									headerHeight = $('div.pop_container div.header').css('height').replace(/px/,'')*1;
									headerTopPad = $('div.pop_container div.header').css('padding-top').replace(/px/,'')*1;
									headerBotPad = $('div.pop_container div.header').css('padding-bottom').replace(/px/,'')*1;
									backGroundTopPad =  $('div.backer').css('padding-top').replace(/px/,'')*1;
									bodyTopPosOffset = bodyTopPad+headerHeight+headerTopPad+headerBotPad+backGroundTopPad;
									bodyVertical = bodyHeight+bodyTopPosOffset+scrollHeight;
									if(bodyVertical > h)
									{
										manualAdjust = 25;
										difference = bodyVertical-h;
										adjustedBody = bodyHeight-difference-manualAdjust;
										//adjustedBody = 254;
										$('div.pop_content').css('height',adjustedBody);
										//alert('Adjustedbody'+adjustedBody+'wrapper height: '+h+' bodyTopPad '+bodyTopPad+' Top Offset: '+ bodyTopPosOffset+' Vertical: '+bodyVertical);
									
									}
									
									
								}
								else if($('div.pc_design').length)
								{
									var h = elts.contentWrapper.height();
									bodyHeight = $('div.pop_content').css('height').replace(/px/,'')*1;
									scrollHeight=10;
									bodyTopPad = $('div.pop_content').css('padding-top').replace(/px/,'')*1;
									headerHeight = $('div.pop_container div.header').css('height').replace(/px/,'')*1;
									headerTopPad = $('div.pop_container div.header').css('padding-top').replace(/px/,'')*1;
									headerBotPad = $('div.pop_container div.header').css('padding-bottom').replace(/px/,'')*1;
									backGroundTopPad = 0;
									bodyTopPosOffset = bodyTopPad+headerHeight+headerTopPad+headerBotPad+backGroundTopPad;
									bodyVertical = bodyHeight+bodyTopPosOffset+scrollHeight;
									if(bodyVertical > h)
									{
										manualAdjust = 0;
										difference = bodyVertical-h;
										adjustedBody = bodyHeight-difference-manualAdjust;
										$('div.pop_content').css('height',adjustedBody);
										//alert('Adjustedbody'+adjustedBody+'wrapper height: '+h+' bodyTopPad '+bodyTopPad+' header height '+headerHeight+' headerTopPad '+headerTopPad+' Top Offset: '+ bodyTopPosOffset+' Vertical: '+bodyVertical);
									
									}
									setControls(block_type);
									
								}
								else if($('div.polaroid').length)
								{
									setControls(block_type);
									
								}
								else if($('div.test_body').length)
								{
									var h = elts.contentWrapper.height();
									bodyHeight = $('div.test_body').css('height').replace(/px/,'')*1;
									scrollHeight=10;
									bodyTopPad = $('div.test_body').css('padding-top').replace(/px/,'')*1;
									headerHeight = $('div.test_header').css('height').replace(/px/,'')*1;
									headerTopPad = $('div.test_header').css('padding-top').replace(/px/,'')*1;
									headerBotPad = $('div.test_header').css('padding-bottom').replace(/px/,'')*1;
									backGroundTopPad =  $('div.pop_background').css('padding-top').replace(/px/,'')*1;
									bodyTopPosOffset = bodyTopPad+headerHeight+headerTopPad+headerBotPad+backGroundTopPad;
									//bodyTopPosOffset = $('div.test_body').offset();
									//bodyTopPosOffset = bodyTopPosOffset.top*1;
									bodyVertical = bodyHeight+bodyTopPosOffset+scrollHeight;
									if(bodyVertical > h)
									{
										manualAdjust = 25;
										difference = bodyVertical-h;
										adjustedBody = bodyHeight-difference-manualAdjust;
										//adjustedBody = 254;
										$('div.test_body').css('height',adjustedBody);
										//alert('Adjustedbody'+adjustedBody+'wrapper height: '+h+' bodyTopPad '+bodyTopPad+' Top Offset: '+ bodyTopPosOffset+' Vertical: '+bodyVertical);
									
									}
									
								}
								
								
								
								
							
							},
							
							endShowContent: function(elts, settings) 
							{
								removeNYROFilter();
								if(block_type != false)//Block type modal will load privacy modal in if privacy link is clicked afterwards -s disable block after each call to allow privacy modal as a pop up
								{
									var modalHTML = $('div#nyroModalContent').html();
									$.nyroModalRemove();
									$('div.polaroid_frame').html(modalHTML);
									$('a.open_nyro_block').not('a.abs_disabled').fadeIn(700,function(){//looks elegant - fading in with modal content - and prevents user from clicking new nyro content before this one fully loaded
										$('a.open_nyro_block').removeClass('nyro_disabled');//once fade finished reove disabling class to allow click functionality												
																							//700ms seems enough to allow nyro to finish - any less and user might break it by clicking too fast
																										});
									
									
									
									
								}
								scroller();
							}
						   });//nyroModalManual
						
	
	
}

function removeNYROFilter()
{
	//nyromodal distorts text in IE7 so remove filter
	if($.browser.msie)
	{
		$('#nyroModalWrapper').get(0).style.removeAttribute('filter');
	}	
}


function setControls(block_type)
{
		if($.browser.msie || block_type != false)
		{
			
			if(current_gallery+1 == gallery_count)
			{
				$('div.abs_next').addClass('abs_disabled');
				$('p.next a').attr('href','#');
				
			}
			else
			{
				$('div.abs_next').removeClass('abs_disabled');
				$('a[rel*=next]').attr('href',urls[current_gallery+1]);//only necessary for IE it seems which chokes on server side set HREF
				
			}
			if(current_gallery-1 < 0)
			{
				$('div.abs_prev').addClass('abs_disabled');
				$('p.previous a').attr('href','#');
			}
			else
			{
				$('div.abs_prev').removeClass('abs_disabled');
				$('a[rel*=prev]').attr('href',urls[current_gallery-1]);	
			}
			$('p.count').html((current_gallery+1)+' of '+gallery_count);
		}
		if(block_type == false)//set next prev events for buttons inside nyromodal (not polaroid modals)
		{
			$('a[rel*=next]').click(
			function()
			{
				this_loc = $(this).attr('href');
				setNextEvent(this_loc,false);
				return false;
			});
								   
			$('a[rel*=prev]').click(
			function()
			{
				this_loc = $(this).attr('href');
				setPrevEvent(this_loc,false);
				return false;
			});
		}
		//setNextEvent(block_type);
		//setPrevEvent(block_type);		
			
	
}

function setNextEvent(this_loc,block_type)
{
	
	if(current_gallery+1 == gallery_count)return false;
	if(current_gallery+1 < gallery_count)
	{
		current_gallery++;
	}
	//alert('LOC: '+this_loc+' current gal: '+current_gallery+' gal length: '+current_gallery)
	openfaceBox(this_loc,0,0,block_type);

}

function setPrevEvent(this_loc,block_type)
{
			
	if(current_gallery-1 <0)return false;
	if(current_gallery-1 > -1)
	{
		current_gallery--;
	}
	//alert('LOC: '+this_loc+' current gal: '+current_gallery+' gal length: '+current_gallery)
	openfaceBox(this_loc,0,0,block_type);
	
}

/*function setNextEvent(block_type)
{
	
	$('a[rel*=next]').click(
		function()
		{
			
			if(current_gallery+1 == gallery_count)return false;
			this_loc = $(this).attr('href');
			if(current_gallery+1 < gallery_count)
			{
				current_gallery++;
			}
			alert('current gal: '+current_gallery+' gal length: '+current_gallery)
			openfaceBox(this_loc,0,0,block_type);
			return false;
		}
							   
	);	
	
}
function setPrevEvent(block_type)
{
	$('a[rel*=prev]').click(
		function()
		{
			
			if(current_gallery-1 <0)return false;
			if(current_gallery-1 > -1)
			{
				current_gallery--;
			}
			this_loc = $(this).attr('href');
			openfaceBox(this_loc,0,0,block_type);
			
			return false;
		}
							   
	);		
	
}*/

function scroller()
{
	
		// this initialises the demo scollpanes and makes it reinitialise itself once it's images have loaded...
		$('div.test_body')
			.jScrollPane(
				{
					showArrows:true, 
					scrollbarWidth: 5,
					arrowSize:6,
					reinitialiseOnImageLoad: true
				}
			);	
		
		
		$('div.pop_content')
			.jScrollPane(
				{
					showArrows:true, 
					scrollbarWidth: 5,
					arrowSize:6,
					reinitialiseOnImageLoad: true
				}
			);
			
		
	
}
