/* HDFF, Copyright 2011. */


Number.prototype.formatCurrency = function(d_space, d_char, t_char) {
	var iNumber = this;
	var d_space = isNaN(d_space = Math.abs(d_space)) ? 2 : d_space;
	var d_char = d_char == undefined ? "," : d_char;
	var t_char = t_char == undefined ? "." : t_char;
	var m_char = iNumber < 0 ? "-" : "";
	var t1_str = parseInt(iNumber = Math.abs(+iNumber || 0).toFixed(d_space)) + "";
	var t2_num = (t2_num = t1_str.length) > 3 ? t2_num % 3 : 0;
	return m_char + (t2_num ? t1_str.substr(0, t2_num) + t_char : "") + t1_str.substr(t2_num).replace(/(\d{3})(?=\d)/g, "$1" + t_char) + (d_space ? d_char + Math.abs(iNumber - t1_str).toFixed(d_space).slice(2) : "");
};

var ajaxLoader = function() {
	var lv_module_content_box_name = "kkm_site_content";
	var lv_module = {id:new guifw.ajaxobject(), cbox:null}; 
	var lv_multi = {id:new guifw.ajaxobject(), cbox:null};
	var fn_ajax_module_request_callback = function() {
		if (lv_module.id.readystate() == 4 && (lv_module.id.status() == 200 || window.location.href.indexOf("http") == -1)) {
			if (lv_module.cbox != undefined && lv_module.cbox != null) {
				lv_module.cbox.style.background = "none";
				if (lv_module.id.responsetext() != "") {
					lv_module.cbox.innerHTML = lv_module.id.removescripts();
					lv_module.id.runscripts();
				} else {
					lv_module.cbox.innerHTML = "";
				}
			}
		}
	}
	var fn_ajax_multi_request_callback = function() {
		if (lv_multi.id.readystate() == 4 && (lv_multi.id.status() == 200 || window.location.href.indexOf("http") == -1)) {
			if (lv_multi.cbox != undefined && lv_multi.cbox != null) {
				lv_multi.cbox.style.background = "none";
				if (lv_multi.id.responsetext() != "") {
					lv_multi.cbox.innerHTML = lv_multi.id.removescripts();
					lv_multi.id.runscripts();
				} else {
					lv_multi.cbox.innerHTML = "";
				}
			}
		}
	}
	return {
		module:function(i_path, i_parm) {
			if (i_path != undefined && i_path != "") {
				var t_date = new Date();
				var t_urlpath = "rep/" + i_path + "?rnd=" + t_date.getTime();
				lv_module.cbox = guifw.eid(lv_module_content_box_name);
				lv_module.cbox.innerHTML = "";
				lv_module.cbox.style.background = "url(img/kkm_ajax_animloader.gif) no-repeat center center";
				lv_module.id.post(t_urlpath, i_parm, fn_ajax_module_request_callback);
			}
		},
		modal:function(i_modal, i_path, i_parm, i_timer) {
			if (i_modal != undefined && i_modal != null && i_modal != "" && i_path != undefined && i_path != "") {
				var t_timer = 5000;
				var t_parm = "";
				if (i_parm != undefined && i_parm != null && i_parm != "") t_parm = i_parm;
				if (i_timer != undefined && i_timer != null) {
					t_timer = parseInt(i_timer);
					if (isNaN(t_timer)) t_timer = 5000;
				}
				kkmModalWindowDisplay(i_modal);
				guifw.modalwindow.setItemBackground(i_modal, {image:"img/kkm_ajax_animloader.gif"});
				guifw.modalwindow.setItemContent(i_modal, {urlpath:i_path, urlparm:t_parm, timeout:t_timer});
			}
		},
		multi:function(i_obj, i_path, i_parm) {
			if (i_obj != undefined && i_obj != null && i_path != undefined && i_path != "") {
				lv_multi.cbox = i_obj;
				lv_multi.id.post(i_path, i_parm, fn_ajax_multi_request_callback);
			}
		}
	}
}();

var kkmMenuScrollSynchronization = function() {
	var lv_cbwin = null;
	var lv_mpwin = null; 
	var fn_menu_canvas_scroll = function() {
		if (lv_cbwin != undefined && lv_cbwin != null && lv_mpwin != undefined && lv_mpwin != null) {
			if (lv_cbwin.scrollTop > 68) {
				lv_mpwin.style.top = (lv_cbwin.scrollTop + 4) + "px";
			} else {
				lv_mpwin.style.top = "68px";
			}
		}
	}
	return {
		init:function() {
			lv_cbwin = guifw.eid("kkm_site_content");
			lv_mpwin = guifw.eid("kkm_menu_canvas");
			if (lv_cbwin != undefined && lv_cbwin != null && lv_mpwin != undefined && lv_mpwin != null) {
				fn_menu_canvas_scroll();
				guifw.events.add(lv_cbwin, "scroll", fn_menu_canvas_scroll);
			}
		}
	}
}();

function kkmModalWindowDisplay(i_name, i_parm) {
	if (i_name != undefined && i_name != null && i_name != "") {
		var t_parm = {width:40, height:40, resize:"off"};
		if (i_parm != undefined) {
			if (i_parm.width != undefined && i_parm.width != null) t_parm.width = i_parm.width;
			if (i_parm.height != undefined && i_parm.height != null) t_parm.height = i_parm.height;
			if (i_parm.resize != undefined && i_parm.resize != null) t_parm.resize = i_parm.resize;
		}
		if (!guifw.modalwindow.active()) {
			guifw.modalwindow.open({color:"#FFFFFF", alpha:75});
		} else {
			guifw.modalwindow.showItems(0);
		}
		if (guifw.modalwindow.itemExist(i_name)) {
			guifw.modalwindow.resizeItem(i_name, {width:t_parm.width, height:t_parm.height, resize:t_parm.resize});
			guifw.modalwindow.activeItem(i_name, 1);
		} else {
			guifw.modalwindow.addItem(i_name, {width:t_parm.width, height:t_parm.height, resize:t_parm.resize, background:"none"});
		}
	}
}

function kkmOpenLinkWindow(i_path) {
	if (i_path != undefined && i_path != null && i_path != "") {
		var t_bwin = null;
		var t_wprm = "";
		var t_height = screen.height - 160;
		var t_width = screen.width - 160;
		if (t_height < 200) t_height = 200;
		if (t_width < 200) t_width = 200;
		t_wprm = "width=" + t_width + ", height=" + t_height + ", top=40, left=70";
		t_wprm += ", directories=no, location=no, menubar=no, resizable=yes, scrollbars=yes, status=yes, toolbar=no";
		t_bwin = window.open(i_path, "_blank", t_wprm);
 		if (window.focus) t_bwin.focus();
	}
}

function kkmDocumentView(i_path, i_title) {
	if (i_path != undefined && i_path != null && i_path != "") {
		var t_browser = guifw.browsersize();
		var t_height = t_browser.clientHeight - 40;
		var t_width = t_browser.clientWidth - 40;
		if (t_height < 100) t_height = 100;
		if (t_width < 400) t_width = 400;
		t_parm = "w_width=" + t_width + "&w_height=" + t_height;
		if (i_title != undefined && i_title != null && i_title != "") {
			t_parm += guifw.urlencode(t_parm, "w_title", i_title);
		}
		t_parm += guifw.urlencode(t_parm, "w_urlpath", i_path);
		ajaxLoader.modal("kkmviewer", "rep/kkm_contentview.php", t_parm);
	}
}

function kkmSiteLoader(i_action) {
	if (i_action != undefined && i_action != null) {
		switch (i_action) {
			case "101": // Aktualnosti
				ajaxLoader.module("kkm_aktualnosti.php");
				break;

			case "201": // O Klubu
				ajaxLoader.module("kkm_oklubu.php");
				break;

			case "202": // O Klubu - Naši treneri
				ajaxLoader.module("kkm_treneri.php");
				break;

			case "203": // O Klubu - Članovi kluba
				ajaxLoader.module("kkm_clanovi.php");
				break;

			case "204": // O Klubu - Revije na ledu
				ajaxLoader.module("kkm_revije.php");
				break;

			case "205": // O Klubu - Raspored treninga
				ajaxLoader.modal("kkmviewer", "rep/kkm_ras_trening.php");
				break;

			case "206": // O Klubu - Raspored natjecanja
				ajaxLoader.module("kkm_ras_natjecanja.php");
				break;

			case "207": // O Klubu - Rezultati natjecanja
				ajaxLoader.module("kkm_rezultati.php");
				break;

			case "208": // O Klubu - Ponuda za suradnju
				ajaxLoader.module("kkm_suradnja.php");
				break;

			case "209": // O Klubu - Ponuda za suradnju
				ajaxLoader.module("kkm_dokumenti.php");
				break;

			case "301": // Škola klizanja
				ajaxLoader.module("kkm_skklizanja.php");
				break;

			case "401": // Galerija
				ajaxLoader.module("kkm_galerija.php");
				break;

			case "501": // Mediji o nama
				ajaxLoader.module("kkm_mediji.php");
				break;

			case "601": // Kontakt
				ajaxLoader.module("kkm_kontakt.php");
				break;
		}
	}
}

function kkmFacebookLoader() {
	kkmOpenLinkWindow("http://www.facebook.com/k.k.medo");
}

function kkmImageLoader(i_type, i_name) {
	if (i_type != undefined && i_type != null) {
		var t_path = "";
		var t_vars = {};
		var t_parm = {};
		if (i_type == 0) {
			guifw.modalwindow.timeout(100);
			return;
		} else if (i_type == 1) {
			t_path = "img/gallery/kkm_large_";
		} else if (i_type == 2) {
			t_path = "img/archive/kkm_mediji_";
		}
		if (i_name != undefined && i_name != "" && t_path != undefined && t_path != "") {
			kkmModalWindowDisplay("photoviewer", {width:910, height:690});
			guifw.modalwindow.setItemContent("photoviewer", {html:"<span id=\"swf_photo_viewer_object\">&nbsp;</span>"});
			t_vars.photopath = t_path + String(i_name).substr(0,3) + ".jpg";
			t_parm.quality = "best";
			t_parm.wmode = "transparent";
			t_parm.menu = "false";
			swfobject.embedSWF("swf/kkm_photoviewer.swf", "swf_photo_viewer_object", 910, 690, "10.0.0", "", t_vars, t_parm);
		}
	}
}

function kkmYouTubeLoader(i_path, i_width, i_height) {
	var t_parm = "";
	if (i_path != undefined && i_width != undefined && i_height != undefined) {
		t_parm = "vd_urlpath=" + i_path + "&vd_height=" + i_height + "&vd_width=" + i_width;
		ajaxLoader.modal("videoviewer", "rep/kkm_videoview.php", t_parm, 10000);
	}
}

function kkmGoogleMapsInit() {
	var t_pos = new google.maps.LatLng(45.814975, 15.984054);
	var t_opt = {zoom:16, center:t_pos, mapTypeId:google.maps.MapTypeId.ROADMAP};
	var t_map = new google.maps.Map(guifw.eid("gmaps_canvas"), t_opt);
	var t_pin = new google.maps.Marker({position:t_pos, map:t_map, title:""});
	var t_nfo = new google.maps.InfoWindow({content:"<span style='font-family:Calibri; font-size:17px; color:#F08000'>Klizački klub MEDO</span><br /><span style='font-family:Calibri; font-size:13px; color:#505050'>Schlosserove stube 2<br />10000 Zagreb</span>"});
	t_nfo.open(t_map, t_pin);
	google.maps.event.addListener(t_pin, "click", function() {t_nfo.open(t_map, t_pin);});
}

