/**
*
* System.js contains the primary methods and functions
*
*/


(function(){

	// create library namespace if not done
	if(!window.VISKOGEN_SE) { window['VISKOGEN_SE'] = {} };
	
	function init(){		
		
		this.debugOn = false;
				
		this.debug({ prefix: 'JS loaded', val: 'true'});
	
		if( this.swf ) this.injectFlash();
		
	};
	VISKOGEN_SE['init'] = init;	
	
	function injectFlash(){		
		
		this.debug('Calling injectFlash()...');
		
		this.debug({ prefix: 'ID', val: this.swf.ID});
		
		this.debug({ prefix: 'height', val: this.swf.height});
		this.debug({ prefix: 'width', val: this.swf.width});
		this.debug({ prefix: 'params', val: this.swf.params});
		for (var key in this.swf.params) this.debug({ prefix: '&nbsp;&nbsp;&nbsp;.' + key.toString(), val: this.swf.params[key]} );
		
		this.debug({ prefix: 'flashvars', val: this.swf.flashvars});
		for (var key in this.swf.flashvars) this.debug({ prefix: '&nbsp;&nbsp;&nbsp;.' + key.toString(), val: this.swf.flashvars[key]} );

		this.debug({ prefix: 'attributes', val: this.swf.attributes});
		for (var key in this.swf.attributes) this.debug({ prefix: '&nbsp;&nbsp;&nbsp;.' + key.toString(), val: this.swf.attributes[key]} );
		
		this.debug({ prefix: 'version', val: this.swf.version});

		this.debug('Injecting flash...');
		if( this.swf.canvas ) swfobject.embedSWF(this.swf.params.swfPath, this.swf.ID, this.swf.width, this.swf.height, this.swf.version,"flash/expressInstall.swf", this.swf.flashvars, this.swf.params, this.swf.attributes);
		this.debug('Flash injected.');
		
	};
	VISKOGEN_SE['injectFlash'] = injectFlash;
	
	
	function shareOnFB(s){		
		
		//u = s || "http://planteratrad.se/fbtest.php";
		
		var share = s ||  window.location.href;
		//this.debug({ prefix: 'sharing ', val: share});
		window.open("http://www.facebook.com/sharer.php?u=" + share , "fbshare", "location=1,status=1,scrollbars=1,width=540,height=436");
	
	
	};
	VISKOGEN_SE['shareOnFB'] = shareOnFB;
	
	function tipFriend(){		
	
		var u = 'http://planteratrad.se/tipsa.php';		
		//this.debug({ prefix: 'tipFriend ', val: u })
		window.open(u, "tipFriend", "location=1,status=1,scrollbars=1,width=350,height=460");
	
	};
	VISKOGEN_SE['tipFriend'] = tipFriend;
	
	function getElementsByAttribute(attribute, attributeValue)
	{
	  var elementArray = new Array();
	  var matchedArray = new Array();
	
	  if (document.all)
	  {
		elementArray = document.all;
	  }
	  else
	  {
		elementArray = document.getElementsByTagName("*");
	  }
	
	  for (var i = 0; i < elementArray.length; i++)
	  {
		if (attribute == "class")
		{
		  var pattern = new RegExp("(^| )" + attributeValue + "( |$)");
	
		  if (pattern.test(elementArray[i].className))
		  {
			matchedArray[matchedArray.length] = elementArray[i];
		  }
		}
		else if (attribute == "for")
		{
		  if (elementArray[i].getAttribute("htmlFor") || elementArray[i].getAttribute("for"))
		  {
			if (elementArray[i].htmlFor == attributeValue)
			{
			  matchedArray[matchedArray.length] = elementArray[i];
			}
		  }
		}
		else if (elementArray[i].getAttribute(attribute) == attributeValue)
		{
		  matchedArray[matchedArray.length] = elementArray[i];
		}
	  }
	
	  return matchedArray;
	};
	VISKOGEN_SE['getElementsByAttribute'] = getElementsByAttribute;
	
	  /**
	 * 
	 * Writes debugging code the a canvas with the ID of 'debug'.
	 * 
	 * 	 
	 * @param prefix
	 *   Something to add before the variable value.
	 * @param val
	 *   A variable value.
	 * @param nobreak
	 *   Optional. If set to true no linebreak will be added after debug text.
	 * @param canvas
	 * 	 Optional. Defines a new canvas for the debugging text to be inserted.
	 *
	 */
	function debug(param){
		
		if( VISKOGEN_SE.debugOn )
		{		
			if(typeof param != 'string')
			{
				data = {};
				data.prefix = param.prefix.toString();
				data.val = param.val ? param.val.toString() : 'null or undefined';
				data.nobreak = param.nobreak ? '' : '<br />';
				data.canvas = param.canvas || 'debug';
				
				if(document.getElementById(data.canvas)) { document.getElementById(data.canvas).innerHTML += data.prefix + ' <var>' + data.val + '</var>' + data.nobreak };
			}
			else
			{
				if(document.getElementById(data.canvas)) { document.getElementById(data.canvas).innerHTML += param + '<br />'};
			}
		}
	};
	VISKOGEN_SE['debug'] = debug;
		  
})();

VISKOGEN_SE.init();
