﻿/* Setup */

var domain = "http://" + document.domain;
var divScale = 2000/333;
var divWidth = jQuery("#menu-main-holder").width();
var theme = 'revolution-29';
var noscriptvar = "no";

var dwmode = "transparent";
if($.browser.mozilla) {
	dwmode = "window"; }
	
var resval = 16;

if($.browser.opera) {
	resval = 4; }

function item_select(object)
{
	var current_value = SWFAddress.getValue().split("/");
	var artist_name = current_value[current_value.length.toString() - 2];
	artist_name = artist_name.replace(/-/g, "_");
	
	$("#menu_" + artist_name + " a").attr('class','');	
	$(object).attr('class','selected');
}

function resize_flash()
{
	divWidth = jQuery("#flashMenu").width();
	divHeight = divWidth*divScale;
	jQuery("#flashMenu").height(divHeight);
	
	if($.browser.msie || $.browser.opera)
	{
		$("#work img").each(function()
		{
			$(this).width($("#work").width() - resval);
		})
	}
}

jQuery(document).ready(function()
{
	var base = domain + "/#/hr/"; // setup base location

	if( base == document.location.toString() ) // $.browser.msie && 
	{
		SWFAddress.setValue("/hr/");
	} // force swfaddress setValue on first load

	divWidth = $("#menu-main-holder").width();
	var divHeight = divWidth*divScale; // get flash container height

	var hasHashvar = false;
	var url = document.location.toString(); // get current url
	var currentUrlvar = "/"; 
	
	if( domain != url && domain + "/" != url ) // if we're not in root
	{
		currentUrlvar = url.split(domain); // get what's after root in url
		
		if( "#" != currentUrlvar[1] && "/#" != currentUrlvar[1] && "/#/" != currentUrlvar[1])
		{
			currentUrlvar = currentUrlvar[1];
		} // if there's not just a hash (#) for our current location
		  // like http://domain#, http://domain/# or http://domain/#/,
		  // take it for our current location
		else
		{
			currentUrlvar = "/"; // else revert to basics
		}
		
		if( "/" != currentUrlvar.substring(0,1) )
		{
			currentUrlvar = "/" + currentUrlvar;
		} // add forward slash if there isn't one
	}
	
	if( "#" == currentUrlvar.substring(1,2) )
	{
		hasHashvar = true;
		var realUrlvar = currentUrlvar;
		currentUrlvar = currentUrlvar.substring(2);
	} // if hash is the first thing after the forward slash
	  // take out the slash and serve it as current url
	else
	{
		hasHashvar = false;

		if( "" != currentUrlvar && "/" != currentUrlvar )
		{
			goto = "/#" + currentUrlvar;
		} // go from /something/nextthing/ to /#/someting/nextthing/
		else
		{
			goto = "/#/hr/";
		} // or go home

		window.location.href = domain + goto; // reload
	}
	
	/* language vars */
	
	getLang = SWFAddress.getValue().split("/");
	lang = getLang[1];

	if( "" == lang )
	{
		lang = "hr";
	}
	
	if( "hr" == lang )
	{
		lang_close = "ZATVORI";
		lang_prev  = "PRETHODNA";
		lang_next  = "SLIJEDEĆA";
		lang_image = "SLIKA";
		lang_of    = "OD";
	}
	
	if( "en" == lang )
	{
		lang_close = "CLOSE";
		lang_prev  = "PREVIOUS";
		lang_next  = "NEXT";
		lang_image = "IMAGE";
		lang_of    = "OF";
	}

	var flashvars = {
		currentUrl: currentUrlvar,
		hasHash: hasHashvar,
		realUrl: realUrlvar,
		noscript: noscriptvar
	};
	var params = {
		menu: "false",
		allowScriptAccess: "always",
		wmode: dwmode,
		salign: "lt"

	};
	var attributes = {
		id: "flashMenu",
		name: "flashMenu"
	};

	swfobject.embedSWF("/menu-main.swf", "menu-main-holder", "100%", divHeight, "9.0.0", "expressInstall.swf", flashvars, params, attributes);
	
	$(window).resize(function()
	{
		resize_flash();
	});
	
	resize_flash();
	
	$("#metarefresh").html("&nbsp;");
})

/* SWFAddress */

function formatTitle(title) {
	
	title = title.split("/");
	var tc = title.length-2;
	title = title[tc];
	title = title.split("-");
	
	var tc2 = title.length;
	var i;
	var newTitle = "";
	
	for(i=0;i<tc2;i++)
	{
		title[i] = title[i].substr(0,1).toUpperCase() + title[i].substr(1).toLowerCase();
		newTitle = newTitle + " " + title[i];
	}
	
    return newTitle + ' / Salon Revolucije - 29. Salon mladih';
}

function getTransport() {
    if (window.XMLHttpRequest) {
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            return new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            return new ActiveXObject('Microsoft.XMLHTTP');
        }
    }
}

function updateChangeArtist(xhr)
{
    if (xhr.readyState == 4)
	{
        if (xhr.status == 200)
		{
            var content = document.getElementById('artist_inner_content');
			content.innerHTML = xhr.responseText;
			$(content).hide(0);
			$(content).fadeIn(1000);
        }
    }
}

function updateChangeWork(xhr)
{
    if (xhr.readyState == 4)
	{
        if (xhr.status == 200)
		{
            var content = document.getElementById('work');
			content.innerHTML = xhr.responseText;
			tb_init('a.thickbox');
			$('a.thickbox').tooltip({ 
				track: false, 
				delay: 0, 
				showURL: false, 
				fade: 250 
			});
			$(content).hide(0);
			$(content).fadeIn(1000);
        }
    }
	
	resize_flash();
}

function handleChange(event)
{
	$("#TB_overlay").remove();
	$("#TB_window").remove();
	$("#TB_load").remove();
	
	getLang = SWFAddress.getValue().split("/");
	lang = getLang[1];
	
    var xhr  = getTransport();
	var xhr2 = getTransport();
	
    xhr.onreadystatechange = function()
	{
		$("#artist_inner_content").fadeOut(500);
        updateChangeArtist(xhr);
    }
    xhr.open('get', domain + '/wp-content/themes/' + theme + '/content-center.php?the_path=' + event.path + '&lang=' + lang, true);
    xhr.send('');
	
	xhr2.onreadystatechange = function()
	{
		$("#work").fadeOut(500);
        updateChangeWork(xhr2);
    }
    xhr2.open('get', domain + '/wp-content/themes/' + theme + '/content-right.php?the_path=' + event.path + '&lang=' + lang, true);
    xhr2.send('');

    SWFAddress.setTitle(formatTitle(event.path));
	
	if( "/hr/publikacije/" == SWFAddress.getValue() || "/en/printed-matter/" == SWFAddress.getValue() )
	{
		resval = 56;
	}
	else
	{
		resval = 16;
	}
	
	getLang = SWFAddress.getValue().split("/");
	
	lang = getLang[1];

	if( "" == lang )
	{
		lang = "hr";
	}
	
	if( "hr" == lang )
	{
		lang_close = "ZATVORI";
		lang_prev  = "PRETHODNA";
		lang_next  = "SLIJEDEĆA";
		lang_image = "SLIKA";
		lang_of    = "OD";
	}
	
	if( "en" == lang )
	{
		lang_close = "CLOSE";
		lang_prev  = "PREVIOUS";
		lang_next  = "NEXT";
		lang_image = "IMAGE";
		lang_of    = "OF";
	}
	
	jQuery("#switchlang").load(domain + "/wp-content/themes/" + theme + "/menu-main-raw.php?language=" + lang);
}

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);