var curimg = null;
var last_curimg = null;
var siteurl = 'http://gitta.cactuscoder.com/';
var contacturl = '/contact.html';
var menu_resize = false;
var ih, iw, src = '';

function scrollbarWidth() {
	var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
    // Append our div, do our calculation and then remove it
	$('body').append(div);
	var w1 = $('div', div).innerWidth();
	div.css('overflow-y', 'scroll');
	var w2 = $('div', div).innerWidth();
	$(div).remove();
	return (w1 - w2);
}

function resizeResolution() {
	var masthead_left = ($('#container').width() > iw) ? $('#container').width() - iw : 0;
	if (masthead_left) {
		if (masthead_left > 235) {
			masthead_left = masthead_left / 2 - 235;
		}
		else {
			masthead_left = 0;
		}
	}
	$('#masthead').css('left', masthead_left+'px');
	// 	resize resolution-sensitive elements
	$('#body-bottom').removeClass('mh-r1 mh-r2 mh-r3').addClass('mmh-'+dr);
	$('div#container,div#content,div#slideshow,div.slideshow-container,div.loader,div.slideshow a.advance-link,#container,.menu,#page_content,#page_content_contact,#page_content_text').removeClass('w-r1 w-r2 w-r3').addClass('w-'+dr);
	$('#container,#content,.slideshow-container,#slideshow,#controls,#page_content').removeClass('h-r1 h-r2 h-r3').addClass('h-'+dr);
	$('#navigation-container,#thumbs,.thumbs').removeClass('th-r1 th-r2 th-r3').addClass('th-'+dr);
	// resize image to fit available area
	$('.w-'+dr).width(iw+'px');
	$('#page_content,#page_content_contact,#page_content_text').width(iw-50+'px');// compensate for padding 50px on left
	var total_height = ih;
	var bottom_height = $('#body-bottom').height();
	var info_height = $('#page_content_contact .info').height();
	var text_height = $('#page_content_text').height();
	var contact_max_height = total_height - bottom_height - info_height - 50 - 50 - 1;
	var text_max_height = total_height - bottom_height - 50 - 50;
	var contact_scroll_height = $('#page_content_contact .scroll').height();
	if (contact_scroll_height < contact_max_height) {
		contact_max_height = contact_scroll_height;
	}
	var text_scroll_height = $('#page_content_text div').height();
	if (text_scroll_height > text_max_height) {
		text_scroll_height = text_max_height;
	}
	var contact_top = (total_height - bottom_height - info_height - 1 - contact_max_height - 1 - 25) / 2;
	var text_top = (total_height - bottom_height - text_scroll_height - 50) / 2;
	$('#page_content_contact .scroll').css('max-height',contact_max_height+'px');
	$('#page_content_contact').css('top', contact_top);
	$('#page_content_text div').css('max-height',text_max_height+'px');
	$('#page_content_text').css('top', text_top);
	$('.h-'+dr).height(ih+'px');
// set menu height
	showThumbs = $('.menu ul li ul li.active,.menu ul li#mi_53.active');
	if (showThumbs[0] != undefined) {
		$('#body-bottom').height(mh+'px');
	}
	// set menu opacity
	$('#body-bottom').fadeTo("fast", 0.8);
	buildPhotoNav();
}

function selectResolution() {
	wh = $(window).height();
	ww = $(window).width() - sw;
	// adjust for width, then height
	// use the image that fits or is bigger than the available area,
	// then size the image down to fit
	if (ww > r2w) {
		if (wh > r2h) {
			rw = r3w;
			rh = r3h;
			dr = 'r3';
		}
		else {
			rw = r2w;
			rh = r2h;
			dr = 'r2';
		}
	}
	else {
		if (wh > r1h) {
			rw = r2w;
			rh = r2h;
			dr = 'r2';
		}
		else {
			rw = r1w;
			rh = r1h;
			dr = 'r1';
		}
	}
	// determine optimum image size
	// first adjust height
	ih = wh;
	iw = ih * rw / rh;
	// then adjust width if necessary
	if (iw > ww) {
		iw = ww;
		ih = iw * rh / rw;
	}
	// finally adjust thumb sizes
	th = ih / 11;
	tw = iw / 11;
// 	mh = (dr=='r3')?mhr3:(dr=='r2')?mhr2:mhr1;
	mmh = (dr=='r3')?mmhr3:(dr=='r2')?mmhr2:mmhr1;
	mh = mmh+th+10;
	resizeResolution();

	if (src != '') {
		var video_height = ih-mmh-5;
		var video_width = video_height*470/320;
		$('#video').css('left', ($('#page_content').width()-video_width)/2);
		var so = new SWFObject(jwbase+'embed/player.swf','mpl',video_width,video_height,'9');
		so.addParam('allowscriptaccess','always');
		so.addParam('allowfullscreen','true');
		so.addParam('wmode','transparent');
		so.addParam('flashvars','&file='+siteurl+src+'&controlbar=over&fullscreen=true&stretching=fill&autostart=true');
		so.write("video");
	}
}

jQuery(function($) {
	if (b.indexOf('ie6') != -1 || b.indexOf('ie7') != -1 || b.indexOf('safari') != -1) {
		// use cookies instead of jStore
		last_curimg = Get_Cookie('curimg');
	}
	else {
		// use jStore
		jQuery.jStore.ready(function(engine){
			last_curimg = jQuery.jStore.store('curimg');
		});
		jQuery.jStore.load();
	}

	sw = scrollbarWidth();
	selectResolution();
	var pc = $("#page_content");
	var html = pc.html();
	if (html) {
		// adjust size and make fully opague
		$("#page_content").css('opacity',1.0).css('z-index',0);
		$("#page_content table").css('opacity',0);
	}
	else {
		$("#page_content").fadeTo("fast", 0.0);
	}
	$(window).bind('resize', function(){
		var bodybottomClass = $('#body-bottom').attr('class');
		if (!menu_resize) {
			selectResolution();
			if (bodybottomClass != 'mmh-'+dr) {
				window.setTimeout(function() {
					document.location.reload();
				}, 1);
			}
		}
		else {
			menu_resize = false;
		}
	});
});

function buildPhotoNav() {
	if (curimg) {
		last_curimg = curimg;
	}
// jQuery(document).ready(function($) {
				// We only want these styles applied when javascript is enabled
	$('div.content').css('display', 'block');

	// insert image thumbs
	jQuery.each(galleryImgs, function(){
		$('#thumbs ul').append('<li class="tw-'+dr+' th-'+dr+'"><a class="thumb" href="/assets/images/gallery/'+galleryFolder+'/'+dr+'/'+this+'" title=""><img src="/assets/images/gallery/'+galleryFolder+'/'+dr+'/t/'+this+'" alt="" /></a></li>');
	});
				// Initially set opacity on thumbs and add
				// additional styling for hover effect on thumbs
	var onMouseOutOpacity = thumb_opacity;
	$('#thumbs ul.thumbs li, div.navigation a.pageLink','#video_thumbs ul.thumbs li').opacityrollover({
	mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
	});

	var gallery_thumbs = $('#thumbs');
				// Initialize Advanced Galleriffic Gallery
	var autostart = (no_gallery) ? false : true;
	gallery = gallery_thumbs.galleriffic({
		delay:                     5000,
		numThumbs:                 thumb_count,
		preloadAhead:              thumb_count,
		enableTopPager:            false,
		enableBottomPager:         false,
		imageContainerSel:         '#slideshow',
		controlsContainerSel:      '#controls',
		captionContainerSel:       '#caption',
		loadingContainerSel:       '#loading',
		renderSSControls:          false,
		renderNavControls:         true,
		playLinkText:              '',
		pauseLinkText:             '',
		prevLinkText:              '',
		nextLinkText:              '',
		nextPageLinkText:          '',
		prevPageLinkText:          '',
		enableHistory:             false,
		autoStart:               	 autostart,
		syncTransitions:           false,
		defaultTransitionDuration: slideshow_transition_duration,
		onSlideChange:             function(prevIndex, nextIndex) {
			var tmpimg = this.data[nextIndex]['slideUrl'].split('/');
			var valid_img = false;
			if (b.indexOf('ie6') != -1 || b.indexOf('ie7') != -1 || b.indexOf('safari') != -1) {
				valid_img = (tmpimg[6] == '') ? false : true;
			}
			else {
				valid_img = (tmpimg[4] == '') ? false : true;
			}
			if (valid_img) {
				curimg = this.data[nextIndex]['slideUrl'];
				curcaption = galleryCaptions[nextIndex];
				// the following causes an error if the menu is being animated at the same time...
				// $('#caption').html(curcaption).css is not a function
				$('#caption').html(curcaption);
				$('#caption').css('bottom', $('#body-bottom').height()+10+'px');
				$('#caption').css('opacity', '0.6');
				if (b.indexOf('ie6') != -1 || b.indexOf('ie7') != -1 || b.indexOf('safari') != -1) {
				// use cookies instead of jStore
					Set_Cookie('curimg', curimg, 0, '/');
				}
				else {
				// use jStore
					jQuery.jStore.store('curimg', curimg);
				}
			}

			this.find('ul.thumbs').children()
				.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
				.eq(nextIndex).fadeTo('fast', 1.0);

					// Update the photo index display
			this.$captionContainer.find('div.photo-index')
				.html('Photo '+ (nextIndex+1) +' of '+ this.data.length);
		},
		onPageTransitionOut:       function(callback) {
			this.fadeTo(slideshow_transition_fadeout, 0.0, callback);
		},
		onPageTransitionIn:        function() {
			var prevPageLink = this.find('a.prev').css('visibility', 'hidden');
			var nextPageLink = this.find('a.next').css('visibility', 'hidden');

					// Show appropriate next / prev page links
			if (this.displayedPage > 0)
				prevPageLink.css('visibility', 'visible');

			var lastPage = this.getNumPages() - 1;
			if (this.displayedPage < lastPage)
				nextPageLink.css('visibility', 'visible');

			this.fadeTo(slideshow_transition_fadein, 1.0);
		}
	});

	if (show_thumbs == '0') {
		$('#thumbs').hide();
// 		$('#body-bottom').removeClass().addClass('mmh-'+dr);
	}
	else {
				/**************** Event handlers for custom next / prev page links **********************/

		gallery.find('a.prev').click(function(e) {
			gallery.previousPage();
			e.preventDefault();
		});

		gallery.find('a.next').click(function(e) {
			gallery.nextPage();
			e.preventDefault();
		});
	}

	if (no_gallery) {
		$("#slideshow").html('<span class="image-wrapper previous" style="opacity: 0.4;"><img alt="" src="'+last_curimg+'"/></span>');
		$('#controls').hide();
		$("#page_content").fadeTo("slow", page_opacity);
	}
}
				/****************************************************************************************/

				/**************** Event handlers for pausing / stopping slideshow **********************/

// $('div.content').hover(
// 	function () {
// 		gallery.pause();
// 	},
// 	function () {
// 		gallery.play();
// 	}
// );

// $("#mi_51,#mi_52").hover(
// function () {
// 	$('.menu ul li ul').addClass('show_2nd_level');
// 	$("#body-bottom").animate({
// 		height: mh
// 	}, 1000 );
// },
// function () {
// }
// );
//
// $("#body-bottom").hover(
// function () {
// },
// function () {
// 	$('.menu ul li ul').removeClass('show_2nd_level');
// 	$("#body-bottom").animate({
// 		height: mmh
// 	}, 1000 );
// }
// );

function menuSize(size,menuid) {
	menu_resize = true;
	switch (size) {
	case 1:
// 		$('.menu ul li ul').removeClass('show_2nd_level');
		$("#body-bottom").animate({
			height: mmh
		}, 1000 );
		break;
	case 2:
			switch (menuid) {
			case 53:
					$("#body-bottom").animate({
						height: mh
					}, 1000 );
				break;
			case 52:
					$("#body-bottom").animate({
						height: mmh
					}, 1000 );
				break;
			default:
				break;
			}
		break;
	}
}

/*
Script Name: Javascript Cookie Script
Author: Public Domain, with some modifications
Script Source URI: http://techpatterns.com/downloads/javascript_cookies.php
Version 1.1.2
Last Update: 5 November 2009

Changes:
1.1.2 explicitly declares i in Get_Cookie with var
1.1.1 fixes a problem with Get_Cookie that did not correctly handle case
where cookie is initialized but it has no "=" and thus no value, the
Get_Cookie function generates a NULL exception. This was pointed out by olivier, thanks
1.1.0 fixes a problem with Get_Cookie that did not correctly handle
cases where multiple cookies might test as the same, like: site1, site
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );

// To use, simple do: Get_Cookie('cookie_name');
// replace cookie_name with the real cookie name, '' are required
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}

	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

