/****************************************************
	* Pixelpost Theme : Almost Eric
	* Template created for Pixelpost 1.7.1
	* Design: Tim Andries (www.almosteric.ca)
	* Code: Julien Roumagnac (www.j-roumagnac.net)
	*
	* Javascript for image_template
	*/
	
window.onload = init;

var comments_panel_visible = false;
var comments_panel_requested = false;
var songinfo_panel_visible = false;

var defaultImgSrc;
var defaultImgWidth;
var defaultImgHeight;
var currentImgSrc;
var currentImgWidth;
var currentImgHeight;

// Initialization
function init() {
	
	// Memorize default image parameters
	defaultImgSrc = document.getElementById('photo').src;
	defaultImgWidth = document.getElementById('photo').width;
	defaultImgHeight = document.getElementById('photo').height;
	
	// Set the current image as the default
	currentImgSrc = defaultImgSrc;
	currentImgWidth = defaultImgWidth;
	currentImgHeight = defaultImgHeight;
	
	// Set the main image container size to the size of the photo
	set_element_size('photo', 'photo_and_comments_container');
	
	// Browser Detection
	BrowserDetect.init();
	
	// Fx.Style declarations
		// Image fade in
		photo_opacity = new Fx.Style('photo','opacity',{duration: 1000, transition: Fx.Transitions.linear, fps: 60}).start(0,1);
		// Comments Panel
		comments_panel_enlargement = new Fx.Style('comments_panel','height',{duration: 750, transition: Fx.Transitions.Cubic.easeInOut, fps: 60, wait: false});
		comments_panel_enlargement.addEvent('onComplete', function(){ 
			if (comments_panel_visible == false) {
				document.getElementById('comments_panel_content').style.overflow = "hidden";
			}
			else {
				fake_ie_scrollhide('comments_panel_content', 'reset');
			}
		});
		comments_panel_enlargement.addEvent('onStart', function(){ 
			if (comments_panel_visible == true) {
				document.getElementById('comments_panel_content').style.overflow = "auto";
			}
			fake_ie_scrollhide('comments_panel_content', 'hide');
		});
		// AJAX Comment callback
		comment_callback_show = new Fx.Style('callback_comment','backgroundColor',{duration: 750, transition: Fx.Transitions.linear, fps: 60});
		//Song info Panel
		songinfo_panel_enlargement = new Fx.Style('songinfo_panel','height',{duration: 750, transition: Fx.Transitions.Cubic.easeInOut, fps: 60, wait: false});
		
	// Comments form submission
	if (document.getElementById('ajax').value == "yes") {
			$('commentform').addEvent('submit', function(e) {			
				new Event(e).stop(); // Stop submit
				
				// Test values before submitting
				if(document.getElementById('message').value != document.getElementById('message').defaultValue && document.getElementById('message').value != "" && document.getElementById('name').value != "") {
				 
					var log = $('callback_comment').empty().addClass('ajax-loading');
		
					this.send({
						update: log,
						onComplete: function() {
							log.removeClass('ajax-loading');
							document.getElementById('message').value = ""; // Empty Textarea
							document.getElementById('callback_comment').style.display = "block"; // Show callback bloc
							comment_callback_show.start("#000000","#454545"); // Fade callback bloc
						}
					});
				}
			});
	}
		
	// Events listeners (alternative comments toggler to flash title)
	if(document.getElementById('comments_toggler')) {
		document.getElementById('comments_toggler').onclick = toggle_comments;
	}

	$$('.songinfo_toggler').each(function(item){
								item.addEvent('click', function(e){
									e = new Event(e).stop();
									toggle_songinfo();
									return false;
								});
							});
	
	// Open comments panel and show thank you after a comment submission
	if (document.location.href.indexOf("#comments") > -1) {
		document.getElementById('thanks_for_comment').style.display = "block";
		document.getElementById('comments_panel').style.height = ae_comments_panel_height + "px";
		document.getElementById('comments_panel_content').style.overflow = "auto";
		comments_panel_visible = true;
		set_close_comments();
		
		// Yes I know this is strange, but I didn't find anything else to fix a display bug in Firefox with anchors
		// The browser seems to  negatively margin "comments_panel_content" the equivalent of the scrolling bar width
		if (BrowserDetect.browser == 'Firefox') {
			window.location = window.location ;
		}
	}
	
}

// Set Target element to the size of Source elements
function set_element_size(sourceEid, targetEid) {
	document.getElementById(targetEid).style.width = document.getElementById(sourceEid).width + "px";
	document.getElementById(targetEid).style.height = document.getElementById(sourceEid).height + "px";
}

// A trick to hide the flickering IE bug 
function fake_ie_scrollhide(targetEid, mode) {
	if(BrowserDetect.browser == "Explorer") {
		e = document.getElementById(targetEid).style;
		
		if(mode == "hide") {
			e.scrollbarBaseColor = "#555555";
			e.scrollbarFaceColor = "#555555";
			e.scrollbarDarkShadowColor = "#555555";
			e.scrollbarHighlightColor = "#555555";
			e.scrollbarShadowColor = "#555555";
			e.scrollbarTrackColor = "#555555";
			e.scrollbarArrowColor = "#DDDDDD";
		}
		if(mode == "reset") {
			e.scrollbarBaseColor = "#454545";
			e.scrollbarFaceColor = "#757575";
			e.scrollbarDarkShadowColor = "#454545";
			e.scrollbarHighlightColor = "#999999";
			e.scrollbarShadowColor = "#646464";
			e.scrollbarTrackColor = "#555555";
		}
	}
}

// Open player popup window
function open_player_popup() {
	window.open( "index.php?x=aepopup&post_id=" + ae_post_id, "aepopup", "location=no, scrollbars=yes, toolbar=no, menubar=no, status=no, height=360, width=367, resizable=no" )
}

// Alternate images
function set_image(targetSrc, targetWidth, targetHeight) {
	targetImg = document.getElementById('photo');
	loadingImg = document.getElementById('loading');
	
	// if the requested image is the current image => do nothing
	if(targetSrc == currentImgSrc) { return false; }

	// Close comments if the panel is open
	if(comments_panel_visible == true) {
		toggle_comments();
		set_default_comments();
	}
	
	// Memorize current image size
	var previousImgWidth = currentImgWidth;
	var previousImgHeight = currentImgHeight;
	
	// Set the requested image as the current image
	currentImgSrc = targetSrc;
	currentImgWidth = targetWidth;
	currentImgHeight = targetHeight;
	
	// Fade out
	img_fadeout = new Fx.Style('photo','opacity',{duration: ae_altimages_transtime, transition: Fx.Transitions.linear, fps: 60}).start(1,0);
	img_fadeout.addEvent('onComplete', function(){ 		
		
		// Display loading
		loadingImg.style.display = "block";
		
		// Force a small size to avoid a IE6 bug (don't want to resize the container to a smaller value because the image is still here)
		targetImg.width = 1;
		targetImg.height = 1;
		
		// Resize container
		img_container_resize = new Fx.Styles('photo_and_comments_container', {duration: ae_altimages_transtime, transition: Fx.Transitions.Cubic.easeInOut, fps: 60, wait: false});
		img_container_resize.start({ 'width': [previousImgWidth, targetWidth], 'height': [previousImgHeight, targetHeight] });
		img_container_resize.addEvent('onComplete', function(){ 
		
			// Change image source
			targetImg.src = targetSrc;
			targetImg.width = targetWidth;
			targetImg.height = targetHeight;
			
			// On load Fade in
			img_fadein = new Fx.Style('photo','opacity',{duration: ae_altimages_transtime, transition: Fx.Transitions.linear, fps: 60});
			img_fadein.addEvent('onComplete', function(){ 
				
				// if comments panel was requested => toggle it !
				if(comments_panel_requested == true) {
					comments_panel_requested = false;
					toggle_comments();
				}
			});
			
			targetImg.onload = function() {
				
				// remove loading
				loadingImg.style.display = "none";
				
				img_fadein.start(0,1);
			}
		
		});
	});
}

// Comments Toggle
function toggle_comments() {
	
	// If the current image isn't the default image => reset to the default image
	if(currentImgSrc != defaultImgSrc && ae_altimages_backtodi == true) {
		comments_panel_requested = true;
		set_image(defaultImgSrc, defaultImgWidth, defaultImgHeight);
		return false;
	}
	
	if (comments_panel_visible == false) { 
		comments_panel_visible = true;
		comments_panel_enlargement.start(ae_comments_panel_height);
	}
	else {
		comments_panel_visible = false;
		comments_panel_enlargement.start(0);
	}
}

// Songinfo Toggle
function toggle_songinfo() {
	
	if (songinfo_panel_visible == false) { 
		songinfo_panel_visible = true;
		songinfo_panel_enlargement.start($('songinfo_panel_content').getStyle('height'));
	}
	else {
		songinfo_panel_visible = false;
		songinfo_panel_enlargement.start(0);
	}
}

// Toggle the Flash title from "has X comments" to "close comments"
function set_close_comments() {
	document.getElementById('ae_flash_title').setCloseComments();
}

// Toggle the Flash title from "close comments" to "has X comments"
function set_default_comments() {
	document.getElementById('ae_flash_title').setDefaultComments();
}

// Browser detection
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};