var load_category_int = 0;
var load_photo_int = 0;
var visible_category_int = 0;
var ideakuvasto_loaded_bool = false;
var loading_cat_id = 0;

//$(document).ready( function() {
function lightbox_readyfy() {
	//Requires jQuery 1.2++

	show_cursor_busy();
	parse_ideakuvasto_anchor();

	show_cursor_default();

}
			
/* Lightbox JS */

var f = 0;

function toggle_lightbox(toimipaikka) {
	
	var visible = 0;
	
	var w = $(window);
	var d = $(document);
	
	var o = $("#overlay");
	var l = $("#lightbox");
	
	// Toggle map visibility
	o.toggle();
	l.toggle();
	
	load(toimipaikka);
}

function preload_images() { //jquery preload
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function parse_ideakuvasto_anchor() {
	/* Check URL Anchor params - to show ideakuvasto or not? */
	var	query_hash_pars = function () {
		// Anon func, the return value is array of URL-parameters
		var query_string = {};
		var query = window.location.hash.substring(1);

		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) {
			var pair = vars[i].split("=");
			query_string[pair[0]] = pair[1];
		} 
		return query_string;
	} ();
	
	//resolve view and category
	if ( window.location.hash.indexOf("ideakuvasto") != -1 ) { //jos param ideakuvasto, ideakuvasto esiin
		//if tuotekategoria
		if ( query_hash_pars['kategoria'] ) { //jos param tuotekategoria, näytä
			load_category_int = query_hash_pars['kategoria'];
		} 
		//if tuote 
		if ( query_hash_pars['tuote'] ) { //jos param tuote, näytä
			load_photo_int = query_hash_pars['tuote'];
		}
		show_ideakuvasto();
	} // /if params
}

function show_ideakuvasto() {
	//optional arguments: arguments[0] == kategoria, arguments[1] == tuote
	//optional arguments override url!
	if (typeof(arguments[0]) != 'undefined') {
		hsh_str = "#ideakuvasto&kategoria=" + arguments[0];

		if (typeof(arguments[1] != 'undefined')) {
			hsh_str += "&tuote=" + arguments[1];
		}

		window.location.hash = hsh_str;
		
		load_category_int = arguments[0];
		load_photo_int = arguments[1];
	}
	//Requires jQuery 1.2+ && getDocHeight()
	var overlay_height =  getDocHeight() + "px";
	$("#ideakuvasto_overlay").css("height", overlay_height); //overlay_height);

	$("div#ideakuvasto_overlay").removeClass("hidden");
	$("div#ideakuvasto_lightbox").removeClass("hidden");

	if ( ideakuvasto_loaded_bool == false ) {
		load_ideakuvasto();
	}
}

function hide_ideakuvasto() { 
	$("div#ideakuvasto_lightbox").addClass("hidden");
	$("div#ideakuvasto_overlay").addClass("hidden");
	window.location.hash = "#";

	load_category_int = 0;
	load_photo_int = 0;
	visible_category_int = 0;
	loading_cat_id = 0;
}

function load_ideakuvasto() {
	var optionalParams = window.location.hash.substring("#ideakuvasto".length + 1);
	$("div#ideakuvasto_lightbox iframe").attr("src", "/ideakuvasto_09" + ( optionalParams.length > 0 ? "?" + optionalParams : "" ));
	$("div#ideakuvasto_lightbox").show();//load("http://" + window.location.hostname + "/ideakuvasto_09 div#ideakuvasto_wrap", loaded_ideakuvasto);
}

function loaded_ideakuvasto() {
	//anchor check?
	if (window.location.hash.substring(1, 5) != "idea") {
		window.location.hash = "#ideakuvasto";
	}

	//EVENT binding
	//ideak category link style event
	$("li.kategoria a").live('click', function() {
		$("li.kategoria a").removeClass("active");
		$(this).addClass("active");
	});
	//ideak gal thumbnail framing event
	$("td.thumbcell a").live('click', function() {
		$("img.thumbframe").hide();
		$("img.thumbframe", this).show();
	});
	//ideak gal photo buttons display event
	$("div.etusivu_button").live('focus', function() {
		alert("Button HERE!");
	});

	/* Ideakuvasto Front House Hovers */
	$("div.idea_hoverer")
		.hover( 
			function() { //onHover
				$("img", this).show(); //.css("width", "125%").css("height", "125%").animate({ width: "100%", height: "100%"}, 1000 );
				//TODO category highlight on hover
				},
			function() { //out hover
				$("img", this).hide();
				} ); // /hover
	preload_images("/images/ideakuvasto/tausta.png", "/images/ideakuvasto/number_1.gif", "/images/ideakuvasto/number_2.gif","/images/ideakuvasto/number_3.gif","/images/ideakuvasto/number_4.gif","/images/ideakuvasto/number_5.gif","/images/ideakuvasto/number_6.gif","/images/ideakuvasto/number_7.gif","/images/ideakuvasto/number_8.gif","/images/ideakuvasto/number_9.gif","/images/ideakuvasto/number_10.gif","/images/ideakuvasto/number_11.gif");

	// / EVENT binding

	ideakuvasto_loaded_bool = true;
	//view_idea_category( load_category_int ); //view ik etusivu
} // /loaded_ideakuvasto

function getDocHeight() {
    var D = document;
		var min_height_int = 920;
    return Math.max(
			min_height_int,
			Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
      Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
      Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function load_ideakuvasto_category( cat_id ) {
	var request_url_str = "http://" + window.location.hostname + "/ideakuvasto_09?request_category=" + cat_id;
	//load cat gall and append to DOM
	$.get(request_url_str, function(data) {
		$(data).appendTo("div#galleries"); 
		loading_cat_id = cat_id; 
		loaded_ideakuvasto_category();
	} );
}

function loaded_ideakuvasto_category() {
	show_idea_category( loading_cat_id ); //view ik etusivu
}

function view_idea_category(cat_id) {
	if ( cat_id == visible_category_int ) {
		return; //jo esillä
	} else if ( cat_id == 0 ) { //etusivu
		show_idea_category(0);
	} else if ( $("#gal_" + cat_id).length > 0 ) { //on jo puussa
		show_idea_category( cat_id );
	} else { //load it
		show_cursor_busy();
		load_ideakuvasto = cat_id;
		load_ideakuvasto_category(cat_id);
	}
}

function show_idea_category(cat_id) {
	//cat_id thumbs exists?
	if ( $("#gal_" + cat_id).length > 0 && visible_category_int != cat_id ) {

		visible_category_int = cat_id;

		$(".kategoria a").removeClass("active");
		$("a[id=" + cat_id + "]").addClass("active");

		//view
		$(".gallery").hide();

		//id_str = "gal_" + cat_id;
		//g_ele = document.getElementById(id_str);
		//g_ele.style.display = "table"; //gives error cant set property
	
		//IE6 show problem - show(), css("display", "table")
		$("[id=gal_" + cat_id + "]").css("display", "block");

		if ( cat_id != 0 ) { //not front
			if ( load_photo_int == 0 ) {
				view_idea_photo(0);
			} else {
				view_idea_photo(load_photo_int);
				load_photo_int = 0; //set to 0 until next time document loads
			}
		}

	} else { //show front

		visible_category_int = 0;
		$("li.kategoria a").removeClass("active");

		$(".gallery").hide();
		$("#gal_0").show();
	}

	show_cursor_default();
}

function set_photo_max_size_attributes(ref) {
	//DEPREC - resize serverside
	photo_maxim_height = 485;
	photo_maxim_width = 690;
	ref = $(ref);
	nav=navigator.appVersion;
	nua=navigator.userAgent;
	str_pos=nua.indexOf('MSIE');
	nu=nua.substr((str_pos+5),3);
	ie=((nua.indexOf('MSIE')!=-1)&&!(nua.indexOf('Opera')!=-1));
	ie6=(ie&&(nu.substring(0,1)==6));

	if ( ie && ie6 ) {
		//if ( ref.height() > photo_maxim_height ) {
		//	ref.attr("height", "485");
		//}
		//if ( ref.width() > photo_maxim_width ) {
		//	ref.attr("width", "690");
		//}
	}
}

function view_idea_photo( prod_id ) {
	show_cursor_busy();
	//tgt_vis_cat = $("[id=gal_" + visible_category_int + "]");
	tgt_product = $("div#product_" + +prod_id, "[id=gal_" + visible_category_int + "]" );

	//alert("view photo " + prod_id + " - tgt_cat len " + tgt_vis_cat.length + " - tgt div#product length " + tgt_product.length);	

	if ( prod_id != 0 && tgt_product.length > 0 ) {

		$("div.product_wrap").hide();
		
		tgt_product.show();

		$("img.thumbframe").hide();
		$("div#t_" + prod_id + " img.thumbframe").show();

		document.getElementById("t_" + prod_id).scrollIntoView(false);
		//document.getElementById("ideakuvasto_wrap").scrollIntoView();

	} else { //show first product of visible gallery
		
		$("div.product_wrap").hide();

		tgt_gal = $("[id=gal_" + visible_category_int + "]");

		$(tgt_gal).find("div.product_first").css("display", "block");

		$("[id=gal_" + visible_category_int + "] img.thumbframe").hide();
		$("[id=gal_" + visible_category_int + "] img.thumbframe:first").show();

	}
	loaded_photo();
}

function loaded_photo() {
	show_cursor_default();
}

function show_cursor_busy() {
	$("div#ideakuvasto_lightbox").addClass("busy");
	$("div.lightbox_control").addClass("loading");
}

function show_cursor_default() {
	$("div#ideakuvasto_lightbox").removeClass("busy");
	$("div.lightbox_control").removeClass("loading");
}
