// FLASH <-> JS <-> DRUPAL

jQuery.fn.wrapInTable = function() {
		var content = $(this);;
        var table = $('<table><tr><td class="contentHere"></td></tr></table>');
        table.insertBefore(content);
        table.find('td.contentHere').append(content);
};



get_i18n_flags = function(block_id){
	var html='';
	var cur=undefined
	$(block_id + ' .i18n-link a').each(function(x){
		if(this.href != cur){	
			html+= '<a href="'+this.href+'">'+this.innerHTML+'</a>'
		}										
		cur=this.href
    });
	//html = html.replace(/IMG/g,'img');
	return html;
}

get_i18n_flags_html = function(block_id,direction){
	var html='';
	var curr=undefined
	var hrefs=new Array
	var languages=new Array
	$(block_id + ' .i18n-link a').each(function(x){										
		if(this.href != curr){	
			hrefs.push(this.href)
		}										
		curr=this.href
    });
	$(block_id + ' .i18n-link a img').each(function(xx){
		languages.push(this.alt + ' ')
    });
	for (var i = 0; i <= hrefs.length-1; i++) {
		html+= '<a href="'+hrefs[i]+'">'+languages[i]+'</a>'
		if(direction=="v"){
			html+="<br>"
		}
	}
	return html;
}
get_i18n_languages = function(block_id){
	var curr=undefined
	var arr=new Array
	arr={}
	var hrefs=new Array
	var languages=new Array
	$(block_id + ' .i18n-link a').each(function(x){										
		if(this.href != curr){	
			hrefs.push(this.href)
		}										
		curr=this.href
    });
	$(block_id + ' .i18n-link a img').each(function(xx){
		languages.push(this.alt)
    });
	for (var i = 0; i <= hrefs.length-1; i++) {
		arr[i]=new Array
		arr[i]={language:languages[i],href:hrefs[i]}
	}
	return arr;
}



// flash call: exchange js object 
function js_object_to_flash(js_object) {
	 return eval(js_object);
}

// flash call: exchange css selector innerHTML with flash
function get_by_selector(selector) {
	var tag = $(selector)[0].tagName
	var inner = $(selector).html()
	if(tag == 'DIV'){
		tag = "p"
	}
	return '<'+tag+'>'+inner+'</'+tag+'>';	
}

// flash call: exchange css selector innerHTML with flash
function get_innerHTML(selector) {
	return encodeURIComponent($(selector).html());
}

// replace div content with flash content
function div_to_flash(div,movie,w,h) {
	var so = new SWFObject(movie+"?"+fVars, div+"_flash", w, h, docData.flash_version, docData.fBackGround);
	so.addParam("allowfullscreen", "true");
	so.write(div);
}

// flash call: get ul link list
get_ul_links_list = function(list_selector,direction){
	var t = $(list_selector+' li').size();
	this.my_html = "";
	for (var i = 0; i <= t-1; i++) {
		this.label = $(list_selector+' li a')[i].innerHTML;
		this.href = $(list_selector+' li a')[i].getAttribute("href");
		if(direction == "horizontal"){
		  this.my_html = this.my_html + '<a href="'+this.href+'">'+this.label+"     <a>"
		}
		if(direction == "vertical"){
		  this.my_html = this.my_html + '<a href="'+this.href+'">'+this.label+"     <a><br>"
		}
	}
	return(this.my_html)
}

// return Drupal url
function getCleanUrl() {
	var wholeURL=document.location //gets the location
	wholeURL=wholeURL + ""  //sets it to a string
	var arr = wholeURL.split("/");
	arr.shift();
	arr.shift();
	arr.shift();
	var queryString = arr.join("/");
	if(queryString){
	  return queryString;
	}
}

// pass queryvars to flash
function query_to_fVars(){
 	wholeURL=document.location
 	wholeURL=wholeURL + ""
 	ending=wholeURL.split('?')
 if(ending[1]){
  	return ending[1];
 }
}

Drupal_get_menuData = function(my_menuId){
	var menu = new Object();
	if($(my_menuId).size() == 1){
		menu.menuId = my_menuId;
		menu.labels = new Array();
		menu.links = new Array();
		menu.classes = new Array();
		menu.types = new Array();
		var t = $(my_menuId + " li").size();
		menu.menu_length = t
		for (var i = 0; i <= t-1; i++) {
			menu.labels.push($(my_menuId + " li a")[i].innerHTML);
			menu.links.push($(my_menuId + " li a")[i].getAttribute("href"));
			menu.classes.push($(my_menuId + " li a")[i].className);
			menu.types.push($(my_menuId + " li")[i].className);
		}
		// get expanded:
		menu.expanded_menus=new Object();
		menu.expanded_menus.menu_length=$(my_menuId + " .expanded").size();
		menu.expanded_menus=Drupal_get_expandedBookMenu(my_menuId+" .expanded");
		//indent
		for (var i = 0; i <= menu.expanded_menus.menu_length-1; i++) {
			for (var ii = 0; ii <= t-1; ii++) {
			if(menu.labels[ii]==menu.expanded_menus.labels[i] && menu.links[ii]==menu.expanded_menus.links[i])
				menu.labels[ii] = docData.menuIndent + menu.labels[ii];
				menu.types[ii] = "expanded";
			}
		}
		return eval(menu);
	}else{
		return false;
	}
}
Drupal_get_expandedBookMenu = function(my_menuId){
	var menu = new Object();
	menu.menuId = my_menuId;
	menu.labels = new Array();
	menu.links = new Array();
	menu.classes = new Array();
	var t = $(my_menuId + " li").size();
	menu.menu_length = t
	for (var i = 0; i <= t-1; i++) {
		menu.labels.push($(my_menuId + " li a")[i].innerHTML);
		menu.links.push($(my_menuId + " li a")[i].getAttribute("href"));
		menu.classes.push($(my_menuId + " li a")[i].className);
	}
	//debug(menu);
	return eval(menu);
}





function toJson(obj) {
 switch (typeof obj) {
  case 'object':
   if (obj) {
    var list = [];
    if (obj instanceof Array) {
     for (var i=0;i < obj.length;i++) {
      list.push(toJson(obj[i]));
     }
     return '[' + list.join(',') + ']';
    } else {
     for (var prop in obj) {
      list.push('"' + prop + '":' + toJson(obj[prop]));
     }
     return '{' + list.join(',') + '}';
    }
   } else {
    return 'null';
   }
  case 'string':
   return '"' + obj.replace(/(["'])/g, '\\$1') + '"';
  case 'number':
  case 'boolean':
   return new String(obj);
 }
}


// CSS <-> FLASH

// flash call: exchange css properties with flash
/*function clone_css_to_flash(selector) {
	my_css_clone = new css_clone(selector);
	return my_css_clone;	
}*/

// clone css object
function css_clone(selector) {
	// flash supported css
	this.css_properties = new Array("text-align","font-size","leading","text-decoration","margin-left","margin-right","font-weight","kerning","font-style","letter-spacing","text-indent","font-family","color")
	// extra properties
	this.css_properties.push("left")
	this.css_properties.push("top")
	this.css_properties.push("width")
	this.css_properties.push("height")
    this.css_properties.push("background-color")
	this.css_properties.push("background-image")
	this.css_values = new Array();
	var t = this.css_properties.length; 
    for (var i = 0; i < t; i++) {
	// get absolute dimensions
	  if(this.css_properties[i] == "width"){
		  this.css_values.push($(selector).width()+"px")
	  }else{
		  if(this.css_properties[i] == "height"){
		     this.css_values.push($(selector).height()+"px")
		  }else{  
		    this.css_values.push($(selector).css(this.css_properties[i]))
	      }
	  }
    } 
}


// resize div container
function resizeDiv(id,w,h) {
	$(id).attr("style","width:"+w);
	$(id).attr("style","min-height:"+h);
	$(id).attr("style","height:auto!important");
	$(id).attr("style","height:"+h);
	// IE fix
	if($.browser.msie) { 
		$( function() { 
			$(id).css("width", w);
			$(id).css("height", h);
		} );
	}
}
function setDivWidth(id,w) {
	$(id).attr("style","width:"+w+";");
	// IE fix
	if($.browser.msie) { 
		$( function() { 
			$(id).css("width", w);
		} );
	}
}
function setDivHeight(id,h) {
	$(id).attr("style","min-height:"+h);
	$(id).attr("style","height:auto!important");
	$(id).attr("style","height:"+h);
	// IE fix
	if($.browser.msie) { 
		$( function() { 
			$(id).css("height", h);
		} );
	}
	$(id).css("height", h);
}

//
// UTILITY FUNCTIONS
//

// return total query string
function getQueryString() {
	var wholeURL=document.location //gets the location
	wholeURL=wholeURL + ""  //sets it to a string
	var ending=wholeURL.split('?') //splits the string at the question mark
	if(ending[1]){
 		var queryString = ending[1];
		return queryString;
	}
}

// return query variable
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

// utility functions
function getAbsolutePosition(element) {
    var r = { x: element.offsetLeft, y: element.offsetTop };
    if (element.offsetParent) {
      var tmp = getAbsolutePosition(element.offsetParent);
      r.x += tmp.x;
      r.y += tmp.y;
    }
    return r;
};

// debug
trace = function(message){
	if(drupalData.user == "1"){
	  alert(message)
	}
}
debug = function(object){
	if(drupalData.user == "1"){
	  alert("object = "+dump(object))
	}
}

/**
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
*/
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	if(typeof(arr) == 'object') { //Array/Hashes/Objects
 		for(var item in arr) {
  			var value = arr[item];
 			 if(typeof(value) == 'object') { //If it is an array,
   				dumped_text += level_padding + "'" + item + "' ...\n";
   				dumped_text += dump(value,level+1);
 			 } else {
   				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  			}
 	}
	} else { //Stings/Chars/Numbers etc.
		 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

// IE: BUGFIXES

// for out of memory bug (in case of multiple ExternalInterface calls)

// hack for satisfactory funtioning in IE. Not required for Firefox. 
function clearObjs(obj) {
    var theObj = eval(obj);
    theObj.style.display = "none";
    for (var prop in theObj){
	    if (typeof(theObj[prop]) == "function"){
		    theObj[prop]=null
	    }
	}	
 }
 
// hack for satisfactory funtioning in IE. Not required for Firefox.
function cleanup() {
	if($.browser.msie) { 
	    __flash_unloadHandler = function(){
			externalProbSet = true;
        	if (externalProbSet) {return};
				clearObjs(explorer);
				// tpl
				clearObjs(flashcontent);
				// BUG: flashnode
				clearObjs(undefined);
			if (__flash_savedUnloadHandler != null){
	   		 __flash_savedUnloadHandler();
			}
    	}

    	if (window.onunload != __flash_unloadHandler) { 
			__flash_savedUnloadHandler = window.onunload;
			window.onunload = __flash_unloadHandler;
    	}
	}
}
// hack for satisfactory funtioning in IE. Not required for Firefox.
window.onbeforeunload=cleanup;