/*
client side scripting :: kabukimagic.com
© swingin'sam.designs.com  |  http://www.swinginsam.com
*/

var DOMAIN 		= 'http://' + window.location.host + window.location.pathname;
var PATH 				= '/';

var GALLERY_H = 650;

$(document).ready(function() {
	if(IE) { document.getElementById('ie').value = true; }
	// nav function 
	$nav = $("div#primary_nav");
	$nav.delegate("li a", "click", function() {
		var $curr_hash = window.location.hash.substring(1);
		if($(this).attr("href") != $curr_hash) {
			window.location.hash = $(this).attr("href");
			$("#share_overlay").animate( {"opacity":"0"}, 150, function() {
				$("#share_overlay").hide();
			});
			$(this).blur();
		}
		return false;
	});	
	
	// primary hash change function
	$(window).bind('hashchange',function() {
		$target_hash = window.location.hash.substring(1);
		
		if(!$target_hash) {
			$target_page = window.location.pathname.substring(window.location.pathname.indexOf(PATH) + PATH.length,9999);
			switch($target_page) {
				case 'index.php' : {
					$loc = 'http://' + window.location.host + PATH + 'index.php#index.php';
					break;
				}
				case 'gallery.php' : {
					$loc = 'http://' + window.location.host + PATH + 'index.php#gallery.php' + window.location.search;
					break;
				}
				case 'video.php' : {
					$loc = 'http://' + window.location.host + PATH + 'index.php#video.php' + window.location.search;
					break;
				}
				case 'press.php' : {
					$loc = 'http://' + window.location.host + PATH + 'index.php#press.php' + window.location.search;
					break;
				}
				case 'contact.php' : {
					$loc = 'http://' + window.location.host + PATH + 'index.php#contact.php';
					break;
				}
				default : {
					$loc = 'http://' + window.location.host + PATH + 'index.php#index.php';
				}
			}
			window.location = $loc;
		} 
		
		// hash ok, continue...
		//load target page contents into framework...
		$("#page").find("#content").animate({ opacity: 0.01 }, 300, function() {
			$("#page").load($target_hash + " #xfer", function() { 
				$("#page").find("#content").animate({ opacity: 1.00 }, 300);
				$(document).ready(function() {
					indicate_page();
					kabuki();
					var $target_page = window.location.hash.substring(1,window.location.hash.indexOf('.php'));
					init_subnav($target_page);
					
					// specific page logic ...
			
					if($target_page == 'index') {
						$("div.tooltip").fadeOut(500);
						$('.slideshow').cycle({ fx: 'fade', timeout: 2500, requeueOnImageNotLoaded: true, requeueTimeout: 250 });
						init_social_links();
					}
					if($target_page == 'gallery') {
						$("div.tooltip").fadeOut(500);
						var $target_gallery = $target_hash.substring($target_hash.indexOf('?g=')+3,$target_hash.indexOf('&i'));
						var $gallery_name = $target_gallery.replace("_", " ");
						$("#page").find("#gallery_name").text($gallery_name);
						if(!IE) { window.setTimeout("centre_img()", 1000); }
						if($gallery_name == 'biography') {
							var $target_page = $target_hash.substring($target_hash.indexOf('&i=')+3,9999);
							$("#page").find("div.bio_on").addClass("bio_off");
							$("#page").find("div.bio_on").removeClass("bio_on");
							$("#page").find("div#biography_0"+$target_page).addClass("bio_on");
							$("#page").find("div#biography_0"+$target_page).removeClass("bio_off");
							$("#share").hide();
							$("#hi_res").hide();
						} else {
							$("#share").show();
							$("#hi_res").show();
						}
					}
					if($target_page == 'video') {
						VideoJS.setupAllWhenReady();
						window.setTimeout("centre_vid()", 100);
						$("div.tooltip").fadeOut(500);
					}
					if($target_page == 'press') {
						if(!IE) { window.setTimeout("centre_img()", 1000); }
						$("div.tooltip").fadeOut(500);
					}
					if($target_page == 'contact') {
						$("#content_frame").css( {"background":"rgb(245, 245, 248)" });
						$("#page").find("#em").click(function() {
							window.open("mailto:chuck@kabukimagic.com");																			
						});
						init_social_links();
					}
					
					// end specific page logic.
					$("#util_frame").find("div[title]").tooltip({offset: [-15,0]});
					$("#footer a[title]").tooltip({ offset: [-10, -30]});
				});
			});
		});
	});
	// initial navigation trigger
	$(window).trigger('hashchange');
	
});

function indicate_page() {
	// clear nav indicators and set new active...
	$ie = document.getElementById('ie').value;
	$nav = $("div#primary_nav");
	$nav.find("li").removeClass("on");
	var $target_page = $target_hash.substring(0,$target_hash.indexOf('.php'));
	if($target_page == 'gallery') {
		var $target_gallery = $target_hash.substring($target_hash.indexOf('?g=')+3,$target_hash.indexOf('&i'));
		if($ie == 'false') { $("title").text("kabukimagic.com [" + $target_gallery.replace("_", " ") + "]"); } //ie can't handle the title tag
		$("div#primary_nav").find("li#pn_"+$target_gallery).addClass("on");
	} else if ($target_page == 'video' || $target_page == 'press' || $target_page == 'contact') {
		if($ie == 'false') { $("title").text("kabukimagic.com [" + $target_page + "]"); } //ie can't handle the title tag
		$("div#primary_nav").find("li#pn_"+$target_page).addClass("on");
	} else {
		if($ie == 'false') { $("title").text("kabukimagic.com [home]"); } //ie can't handle the title tag
	}
}

function kabuki() {
	// kabuki trigger listener
	$("div#kabuki").click(function() {
		window.location = "index.php#index.php";
	});
}

function init_social_links() {
	$("#twitter_link").click(function() { window.open("http://twitter.com/KabukimagicNYC"); });
	$("#youtube_link").click(function() { window.open("http://www.youtube.com/user/KabukimagicNYC"); });
}

function init_subnav($target_page) {
	var $sn_type = $("#util").attr('rel');
	//console.log('sn_type :: ' + $sn_type);
	if($sn_type == 'type1') { // numbers based format...
		$("#page").find("#next").click(function() {
			$(this).animate( {"opacity":"0.3"}, 300 );
			$("#share_overlay").animate( {"opacity":"0"}, 150, function() {
				$("#share_overlay").hide();
				do_subnav(1);
			});
		});
		$("#page").find("#next").hover(function() {
			$(this).animate( {"opacity":"1.0"}, 300 );
		}, function() {
			$(this).animate( {"opacity":"0.3"}, 300 );
		});
		$("#page").find("#previous").click(function() {
			$(this).animate( {"opacity":"0.3"}, 300 );
			$("#share_overlay").animate( {"opacity":"0"}, 150, function() {
				$("#share_overlay").hide();
				do_subnav(-1);
			});
		});
		$("#page").find("#previous").hover(function() {
			$(this).animate( {"opacity":"1.0"}, 300 );
		}, function() {
			$(this).animate( {"opacity":"0.3"}, 300 );
		});
		$("#page").find("#hi_res").click(function() {
			window.location = "gallery_fs.php?g="+document.getElementById('curr_g').value;
		});
		$("#page").find("#hi_res").hover(function() {
			$(this).animate( {"opacity":"1.0"}, 300 );
		}, function() {
			$(this).animate( {"opacity":"0.3"}, 300 );
		});
	} else if($sn_type == 'type2') { // thumbnail based format...
		// ********************************************************************
		init_thumbs($target_page);
		// ********************************************************************
		$("#page").find("#next_page").click(function() {
			$(this).animate( {"opacity":"0.3"}, 300 );
			do_paginate(1);
		});
		$("#page").find("#next_page").hover(function() {
			$(this).animate( {"opacity":"1.0"}, 300 );
		}, function() {
			$(this).animate( {"opacity":"0.3"}, 300 );
		});
		$("#page").find("#previous_page").click(function() {
			$(this).animate( {"opacity":"0.3"}, 300 );
			do_paginate(-1);
		});
		$("#page").find("#previous_page").hover(function() {
			$(this).animate( {"opacity":"1.0"}, 300 );
		}, function() {
			$(this).animate( {"opacity":"0.3"}, 300 );
		});
	}
	// inits commons to all types...
	$("#page").find("#share").click(function() {
		$("#share_overlay").show().animate( {"opacity":"1.0"}, 300 );
	});
	$("#page").find("#share").hover(function() {
		$(this).animate( {"opacity":"1.0"}, 300 );
	}, function() {
		$(this).animate( {"opacity":"0.3"}, 300 );
	});
	$("#page").find("#share_overlay #sh_close").click(function() {
		$("#share_overlay").animate( {"opacity":"0"}, 300, function() {
      $("#share_overlay").hide();
  	});
	});
	$("#page").find("#share_overlay #sh_facebook").click(function() {
		$("#share_overlay").animate( {"opacity":"0"}, 300, function() {
      $("#share_overlay").hide();
			do_share('facebook');
  	});
	});
	$("#page").find("#share_overlay #sh_twitter").click(function() {
		$("#share_overlay").animate( {"opacity":"0"}, 300, function() {
      $("#share_overlay").hide();
			do_share('twitter');
  	});
	});
	$("#page").find("#share_overlay #sh_digg").click(function() {
		$("#share_overlay").animate( {"opacity":"0"}, 300, function() {
      $("#share_overlay").hide();
			do_share('digg');
  	});
	});
	$("#page").find("#share_overlay #sh_stumble").click(function() {
		$("#share_overlay").animate( {"opacity":"0"}, 300, function() {
      $("#share_overlay").hide();
			do_share('stumble');
  	});
	});
}

function do_subnav(mult) {
	var curr_hash 				= window.location.hash.substring(0,$target_hash.indexOf('&i=')+4);
	var curr_i 								= document.getElementById('curr_i').value;
	var lim_i 									= document.getElementById('lim_i').value;
	var tar_i 									= parseInt(curr_i) + parseInt(mult);
	if(tar_i <= 0) {
		tar_i = 1;
	}
	if(tar_i >= parseInt(lim_i)+1) {
		tar_i = lim_i;
	}
	var tar_hash 					= curr_hash + tar_i;
	window.location.hash = tar_hash;
}

function centre_img() {
	// centre image vertically for images that are smaller than the framework height...
	var $img_h = $("#content img").height();
	if($img_h < GALLERY_H && $img_h != 0) {
		// calculate the offset and set top margin...
		$os = (GALLERY_H - $img_h)/2
		$("#content img").animate( { 'margin-top': $os+'px' },200 );
	}
}

function centre_vid() {
	// centre video vertically for videos that are smaller than the framework height...
	var $img_h = $("#content #vid_frame").height();
	if($img_h < GALLERY_H && $img_h != 0) {
		// calculate the offset and set top margin...
		$os = (GALLERY_H - $img_h)/2
		$("#content #vid_frame").animate( { 'top': $os+'px' },500 );
	}
}

function do_res() {
	res = document.getElementById('res').value;
	if(res == 0) { // low res, set to high res...
		
		document.getElementById('res').value = 1;
	} else { // high res, resolve to memory...

		document.getElementById('res').value = 0;
	}
}

function do_share(target) {
	var curr_g 	= document.getElementById('curr_g').value;
	var curr_i 		= document.getElementById('curr_i').value;
	if(curr_g == 'video' || curr_g == 'press') {
		var url = DOMAIN + "/index.php#" + curr_g + ".php?g=" + curr_g + "&i=" + curr_i;
	} else {
		var url = DOMAIN + "/index.php#gallery.php?g=" + curr_g + "&i=" + curr_i;
	}
	switch(target) {
		case 'twitter' : {
			xurl = "http://twitter.com/home?status=Currently Viewing " + url;
			break;
		}
		case 'facebook' : {
			xurl = "http://www.facebook.com/sharer.php?u=" + url + "&t=kabukimagic";
			break;
		}
		case 'stumble' : {
			xurl = "http://www.stumbleupon.com/submit?url=" + url + "&title=kabukimagic";
			break;
		}
		case 'digg' : {
			xurl = "http://digg.com/submit?url=" + url + "&title=kabukimagic";
			break;
		}
	}
	window.open(xurl);
	//console.log(target + ' :: ' + url);
}

function init_thumbs($target_page) {
	$("#thumb_list").find("li.tn").hover(function() {
		$(this).animate( {"opacity":"1.0"}, 300 );
	}, function() {
		$(this).animate( {"opacity":"0.8"}, 300 );
	});
	if($target_page == 'video') {
		$("#thumb_list").find("li.tn").click(function() {
			var $req_v = $(this).attr('rel');
			window.location.hash = "video.php?g=video&i="+$req_v+"&p="+document.getElementById('curr_p').value;
		});
	} else if ($target_page == 'press') {
		$("#thumb_list").find("li.tn").click(function() {
			var $req_v = $(this).attr('rel');
			window.location.hash = "press.php?g=press&i="+$req_v+"&p="+document.getElementById('curr_p').value;
		});
		
		// multipage press interactions....
		if(document.getElementById('si_lim').value > 1) {
			var req_si = true;
		} else {
			var req_si = false;
		}
		//console.log('req_si :: ' + req_si);
		//console.log('si_lim :: ' + document.getElementById('si_lim').value);
		//console.log('si :: ' + document.getElementById('si').value);
		if(req_si) {
			if(document.getElementById('si').value <= (document.getElementById('si_lim').value - 1)) {
				//console.log('showing press_next');
				$("#press_next").show().animate( {"opacity":"0.1"}, 500 );
				$("#press_next").hover(function() {
				  $(this).animate( {"opacity":"1.0"}, 500 );
				}, function() {
					$(this).animate( {"opacity":"0.1"}, 500 );
				});
			}
			if(document.getElementById('si').value >= 2) {
				//console.log('showing press_prev');
				$("#press_prev").show().animate( {"opacity":"0.1"}, 500 );
				$("#press_prev").hover(function() {
				  $(this).animate( {"opacity":"1.0"}, 500 );
				}, function() {
					$(this).animate( {"opacity":"0.1"}, 500 );
				});
			}
		}
		//console.log('***');
		// end multipage press interactions
		
		// pdf press interactions....
		if(document.getElementById('pdf').value == "true") {
			$("#press_pdf a").show().animate( {"opacity":"0.25"}, 500 );
			$("#content").hover(function() {
				 $("#press_pdf a").animate( {"opacity":"1.0"}, 500 );
			}, function() {
				$("#press_pdf a").animate( {"opacity":"0.25"}, 500 );
			});
		}
		// end pdf press interactions
	
	}
	var curr_p = document.getElementById('curr_p').value;
	var tn_w			 	= 80;
	var tn_pp 			= 4;
	var page_w		= tn_w*tn_pp;
	var tot_w 			= document.getElementById('lim_i').value * tn_w;
	var curr_p 		= document.getElementById('curr_p').value;
	var num_pages = Math.ceil(tot_w/page_w);
	if(curr_p <= 1) {
		curr_p = 1;
	}
	if(curr_p >= num_pages) {
		curr_p = num_pages;
	}
	var $os = -((parseInt(page_w) * parseInt(curr_p)) - parseInt(page_w));
	$("#thumbs").css( { 'left': $os+'px' } );
	$("#thumb_list li.tn img[title]").tooltip({ offset: [-10, 0], effect: 'slide' }).dynamic({ bottom: { direction: 'down', bounce: true } });
}

function do_paginate(mult) {
	var tn_w			 	= 80;
	var tn_pp 			= 4;
	var page_w		= tn_w*tn_pp;
	var tot_w 			= document.getElementById('lim_i').value * tn_w;
	var curr_p 		= document.getElementById('curr_p').value;
	var num_pages = Math.ceil(tot_w/page_w);
	tar_p							= parseInt(curr_p) + parseInt(mult);
	if(tar_p <= 1) {
		tar_p = 1;
	}
	if(tar_p >= num_pages) {
		tar_p = num_pages;
	}
	document.getElementById('curr_p').value = tar_p;
	var $os = -((parseInt(page_w) * parseInt(tar_p)) - parseInt(page_w));
	$("#thumbs").animate( { 'left': $os+'px' },500 );
}
