var app_path = '';

String.prototype.trim = function () { return this.replace(/^\s*|\s*$/,""); };
String.prototype.underscore = function () { return this.replace(/\s*/,"_"); };

function is_obj(objToTest) { 
// This function checks for valid objects - simpler
    var chk=(objToTest == null || objToTest == undefined)?0:1; return chk;
}


// JavaScript Document
// Onload event listening function
function addLoadListener(fn){
	if (typeof window.addEventListener != 'undefined'){
		window.addEventListener('load', fn, false);
	} else if (typeof document.addEventListener != 'undefined'){
		document.addEventListener('load', fn, false);
	} else if (typeof window.attachEvent != 'undefined'){
		window.attachEvent('onload', fn);
	} else {
		var oldfn = window.onload;
		if (typeof window.onload != 'function'){
			window.onload = fn;
		} else {
			window.onload = function(){
				oldfn();
				fn();
			};
		}
	}
}

function ld_lstnr(fn){
	if (typeof window.addEventListener != 'undefined'){
		window.addEventListener('load', fn, false);
	} else if (typeof document.addEventListener != 'undefined'){
		document.addEventListener('load', fn, false);
	} else if (typeof window.attachEvent != 'undefined'){
		window.attachEvent('onload', fn);
	} else {
		var oldfn = window.onload;
		if (typeof window.onload != 'function'){
			window.onload = fn;
		} else {
			window.onload = function(){
				oldfn();
				fn();
			};
		}
	}
}

function attachEventListener(target, eventType, functionRef, capture) {
	if (typeof target.addEventListener != "undefined") {
		target.addEventListener(eventType, functionRef, capture);
	} else if (typeof target.attachEvent != "undefined") {
		var functionString = eventType + functionRef;
		target["e" + functionString] = functionRef;
		target[functionString] = function(event) {
			if(typeof event == "undefined"){event = window.event}; target["e" + functionString](event);
		};
		target.attachEvent("on" + eventType, target[functionString]);
	} else {
		eventType = "on" + eventType;
		if (typeof target[eventType] == "function") {
			var oldListener = target[eventType];
			target[eventType] = function() {
				oldListener();
				return functionRef();
			}
		} else {
			target[eventType] = functionRef;
		}
	}
	return true;
}

// OBJ
function get_obj(idx){ return document.getElementById(idx); }

// ROLLOVER
function roInit(){
	for(var i=0;i<document.images.length;i++){
		dpattern = /^(ro)/;
		if(document.images[i].parentNode.tagName=="A" && dpattern.test(document.images[i].className)){ roSetup(document.images[i]);	}
	}
}
function roSetup(img){
	img.out = new Image(); img.out.src = img.src;	img.onmouseout = rollOut; img.onclick = rollOut;
	img.over = new Image();	img.over.src = app_path+"/images/"+img.alt+".a.gif"; img.onmouseover = rollOver;
}
function rollOver(){ this.src=this.over.src; }
function rollOut(){ this.src=this.out.src; }
function toggle_layer(id){ if(is_obj(id)){get_obj(id).style.display=(get_obj(id).style.display=='block')?'none':'block'; }}
function set_display(id,dis){	if(is_obj(id)){ get_obj(id).style.display=(dis=='n')?'none':'block'; }}
function set_div(id,dis){	if(is_obj(id)){ get_obj(id).className=(dis=='n')?'hdiv':'sdiv'; }}

function set_opacity(obj, val){	obj.style.opacity = val/10; obj.style.filter = 'alpha(opacity=' + val*10 + ')'; }

function mnu_init(){
  if(is_obj(get_obj('logo'))){
    var b_logo = get_obj('logo');  b_logo.onmouseover=function(){this.style.cursor='pointer';}; b_logo.onclick = function() { document.location = '/'; }
	}
  if(is_obj(get_obj('ft'))){
    var ft_lnk = get_obj('ft');  ft_lnk.onmouseover=function(){this.style.cursor='pointer';}; ft_lnk.onclick = function() { document.location = '/'; }
	}
  if(is_obj(get_obj('collection'))){
    var m_co = get_obj('collection'); //m_co.onclick = function() { document.location = '/collection/ss07/1/'; }
	  m_co.onmouseover = function(){ set_opacity(this, 5); }; m_co.onmouseout = function(){ set_opacity(this, 10); };
    var m_st = get_obj('stockists'); //m_st.onclick = function() { document.location = '/stockists/'; }
	  m_st.onmouseover = function(){ set_opacity(this, 5); }; m_st.onmouseout = function(){ set_opacity(this, 10); };
    var m_pr = get_obj('press'); //m_pr.onclick = function() { document.location = '/press/'; }
	  m_pr.onmouseover = function(){ set_opacity(this, 5); }; m_pr.onmouseout = function(){ set_opacity(this, 10); };
    var m_ab = get_obj('about'); //m_ab.onclick = function() { document.location = '/about/'; }
	  m_ab.onmouseover = function(){ set_opacity(this, 5); }; m_ab.onmouseout = function(){ set_opacity(this, 10); };
    var m_cn = get_obj('contact'); //m_cn.onclick = function() { document.location = '/contact/'; }
	  m_cn.onmouseover = function(){ set_opacity(this, 5); }; m_cn.onmouseout = function(){ set_opacity(this, 10); };
	}
}
ld_lstnr(mnu_init);


// Collection JS
if (typeof set=="undefined"){ set = ''; }
var lg_path = '../gfx/'+set+'/lg/'; var tx_path = '../gfx/'+set+'/tx/';
var a_img = new Array();
var a_len = 0;
var curr_num;

function preload_images(){
	var img_div = document.getElementById("img_pre");
	for (var i=0; i<12; i++){
		im_bg_url = lg_path + a_img[i] + '.jpg';
		im_tx_url = tx_path + a_img[i] + '.gif';
		var im_bg = document.createElement("img"); var im_tx = document.createElement("img");
    im_bg.setAttribute("src",im_bg_url); im_tx.setAttribute("src",im_tx_url);
		img_div.appendChild(im_bg); img_div.appendChild(im_tx);
	}
}
function btn_init(){ 
  var pr = get_obj('pr'); pr.onclick = function(){ go_prev();	};
	pr.onmouseover = function(){ set_opacity(this, 5); }; 
	pr.onmouseout = function(){ set_opacity(this, 10); };
	var nx = get_obj('nx');	nx.onclick = function(){ go_next();	};
	nx.onmouseover = function(){ set_opacity(this, 5); };
	nx.onmouseout = function(){ set_opacity(this, 10); };
}
function go_prev(){	curr_num = (curr_num==0)?a_len:curr_num-1; view_pic(a_img[curr_num]); }
function go_next(){	curr_num = (curr_num==a_len)?0:curr_num+1; 	view_pic(a_img[curr_num]); }
function view_pic(obj_id){ 
	var lg_pic = get_obj('lg'); lg_pic.src = lg_path+obj_id+'.jpg'; 
	if(is_obj('tx')){ var tx_pic = get_obj('tx'); tx_pic.src = tx_path+obj_id+'.gif'; }
}
function th_setup(img, img_num){	
//  img.onmouseover = function(){ set_opacity(this, 7); }; img.onmouseout = function(){ set_opacity(this, 10); };
	img.onclick = function(){	pic_id = img.id; view_pic(pic_id); curr_num = img_num; }
}
function th_first(){ curr_num=0; view_pic(a_img[0]); }
function th_init(){
	ct=0;
	for(var i=0;i<document.images.length;i++){
		dpattern = /^(thmb)/;
  	if(dpattern.test(document.images[i].className)){ 
			th_setup(document.images[i], ct);	
			a_img[ct]=document.images[i].id + '';
			ct++;
		}
	}
	a_len = ct-1; th_first(); btn_init(); preload_images();
}

function go_janezic(){
	var jzc = get_obj('janezic'); jzc.onmouseover=function(){this.style.cursor='pointer';}; jzc.onclick=function(){ window.location='/'; };
}

var scr = null; // Needed for scroll
function stockists_init(){ 
  var el = get_obj("scroller"); scr = new jsScroller(el, 310, 431);
  var scr_up = get_obj("scr_up"); var scr_dn = get_obj("scr_dn");
  scr_up.onmouseover=function(){set_opacity(this, 5); scr.startScroll(0, 5);}; scr_up.onmouseout=function(){set_opacity(this, 10); scr.stopScroll();};
  scr_dn.onmouseover=function(){set_opacity(this, 5); scr.startScroll(0, -5);}; scr_dn.onmouseout=function(){set_opacity(this, 10); scr.stopScroll();};
  //window.onload = function () {var el = get_obj("scroller"); scr = new jsScroller(el, 310, 431);};
}