var hashTag = '';
var hashDelimiter = '#!';

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

$(document).ready(function() {			
	$('.vthumb').click(function() {
		$(this).find('.play').hide();
	});
	
	if (readCookie('age_confirmed'))
	{
		$('.ageRestriction').hide();
		$('.hideAdultContent').show();
	}
	
	$('.ageRestriction').click(function() {

		var answer = confirm($(this).attr('title'));
		if (answer)
		{
			$('.ageRestriction').hide();
			createCookie('age_confirmed', 1, 30);
			$('.hideAdultContent').fadeIn();
		}
		return false;
	});
	
	$('.hideAdultContent').click(function() {
		eraseCookie('age_confirmed');
		location.reload();
	});

	$(window).hashchange(function() {
		var hashParts = window.location.hash.split(hashDelimiter);		
		if (hashParts[1] != hashTag)
		{
			hs.close();
		}
	});
	
	var hashParts = window.location.hash.split(hashDelimiter);
	if (hashParts[1])
	{
		$('.gallery').find('#' + hashParts[1]).click();
	}
});

hs.graphicsDir = 'assets/templates/afs/images/highslide/';
hs.cacheAjax = false;
hs.align = 'center';
hs.allowMultipleInstances = false;
hs.outlineType = null;
hs.showCredits = false;
hs.dimmingOpacity = 1.0;
hs.expandCursor = null;
hs.restoreCursor = null;
hs.transitions = ['fade'];
hs.maxHeight = 815;
hs.preserveContent = false;
hs.numberOfImagesToPreload = 0;

// Fallback for iOS
hs.Expander.prototype.onInit = function() {
	var userAgent = navigator.userAgent.toLowerCase();

	var iPhone = userAgent.indexOf('iphone') != -1;
	var iPod = userAgent.indexOf('ipod') != -1;
	var iPad = userAgent.indexOf('ipad') != -1;

	var iOS = iPhone || iPod || iPad;

	if (iOS)
	{
		location.href = this.a.href;
		return false; // otherwise there's a glitch when user presses "back"
	}
}

// Highslide fixed popup mod. Requires the "Events" component.
if (!hs.ie || hs.uaVersion > 6) hs.extend ( hs.Expander.prototype, {
	fix: function(on) {
		var sign = on ? -1 : 1,
			stl = this.wrapper.style;

		if (!on) hs.getPageSize();
		
		hs.setStyles (this.wrapper, {
			position: on ? 'fixed' : 'absolute',
			zoom: 1, // IE7 hasLayout bug,
			left: (parseInt(stl.left) + sign * hs.page.scrollLeft) +'px',
			top: (parseInt(stl.top) + sign * hs.page.scrollTop) +'px'
		});

		if (this.outline) {
			stl = this.outline.table.style;
			hs.setStyles (this.outline.table, {
				position: on ? 'fixed' : 'absolute',
				zoom: 1, // IE7 hasLayout bug,
				left: (parseInt(stl.left) + sign * hs.page.scrollLeft) +'px',
				top: (parseInt(stl.top) + sign * hs.page.scrollTop) +'px'
			});

		}
		this.fixed = on;
	},
	
	onAfterExpand: function(sender) {
    	this.fix(true);

		hashTag = this.a.id;
		window.location.hash = hashDelimiter + hashTag;
	},
	
	onBeforeClose: function(sender) {
		this.fix(false);
		
		window.location.hash = '!'; // this prevents Safari from scrolling up
		
		$('.play').show();
	}
});
