/* 
	Creates a GUID object
*/
SDN.Utils.Guid = new Class({
	initialize: function(options){
		
		
		this.options = options;
		this.value = (this.getGuidPart()+this.getGuidPart()+"-"+this.getGuidPart()+"-"+this.getGuidPart()+"-"+this.getGuidPart()+"-"+this.getGuidPart()+this.getGuidPart()+this.getGuidPart()).toUpperCase();
	},
	
	getGuidPart : function() {
		return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
	},
	
	toString : function(){
		return this.value;
	}
	
	
})

/*
	Checks to see if the parent element contains the specified
	item
*/
SDN.Utils.Contains = function(parent,item){
	
		var children = parent.childNodes;
		if(children !=null){
		
		for(var i=0;i<children.length;i++){
			var child = children.item(i);
			if(item==child){
				return true;
			}
		}
		
		}
		return false;
	
}

/*
	Removes the children of the specified element
*/
SDN.Utils.Clear = function(el){
	
	while(el.hasChildNodes()) 
		el.removeChild(el.childNodes.item(0));
}

/*
	Sets a Cookie
	Expires value is in Days

*/

SDN.Utils.SetCookie = function ( name, value, expires, path, domain, secure ) 
{
	
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

		document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

/*
	Deletes a Cookie

*/


SDN.Utils.DeleteCookie = function ( name, path, domain ) {
	if ( SDN.Utils.GetCookie( name ) ) document.cookie = name + "=" +( ( path ) ? ";path=" + path : "") +( ( domain ) ? ";domain=" + domain : "" ) +";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

/*
	Gets a cookies value
*/
SDN.Utils.GetCookie = function( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&( name != document.cookie.substring( 0, name.length ) ) )
		{
		return null;
		}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;

	return unescape( document.cookie.substring( len, end ) );
}

/*
	Sets a cookie to store the user selected country
	Country should be in ISO code format.
*/

SDN.Utils.SetCountry  = function (country,language){
		
		
		SDN.Utils.DeleteCookie('sdn_country','/',document.domain,true);
		if(country){
			SDN.Utils.SetCookie('sdn_country',country,50,'/',document.domain);
		}
		var sUrl = window.location.href;

		if(sUrl.indexOf('language=')>0){
			sUrl = sUrl.substring(0,sUrl.indexOf('language=')-1)+sUrl.substring(sUrl.indexOf('language=')+'language=xx'.length);
		}
		
		if(language !=null){		
			if(sUrl.indexOf('?')>0){
				sUrl +='&language='+language;	
			}else{
				sUrl +='?language='+language;
			}
		}
		
		window.location.replace(sUrl);
		
}
/**
Check the text to see whether it contains non-latin characters or not
**/
SDN.Utils.isNonLatin1Characters = function (text) {
   // tests whether at least on non latin 1 letter is found
   // for Latin1 aka ISO 8859-1 charset see http://www.htmlhelp.com/reference/charset/
   // Note: this file have to be stored in UTF-8 because of the special characters the regex expression
   var re = new RegExp("[^ -~¡-ÿ]");     
   if (re.test(text)) {
      return true;
   } else {
      return false;
   }
}

var widgetLibraryLoading = false;
SDN.Utils.loadWidget = function (buzzid, apiToken, divId, disableGoogleAnalytics){
	var doWidgetLoad = function(){
		ng.buzz.LoadWidget(buzzid, apiToken, divId, { disableGoogleAnalytics: disableGoogleAnalytics }, "https://hosted.newsgator.com/ngbuzz/");
	};
	var maxRetriesReached = function(){
		return;
	};

	if ( typeof ng != "undefined" && typeof ng.buzz != "undefined" ){
		doWidgetLoad();
		return;
	}
	if (!widgetLibraryLoading) {
		//create script element to load library
		var scriptElem = document.createElement("SCRIPT");
		scriptElem.src = "https://hosted.newsgator.com/ngbuzz/load.ashx/buzz";
		scriptElem.type = "text/javascript";
		document.getElementsByTagName("HEAD")[0].appendChild(scriptElem);
		
		widgetLibraryLoading = true; 
	}
	//Set interval to check whether script is loaded
	var retries = 0;
	var statusInterval = setInterval(function() {
		//Check the number of retries and only allow 3 retries so that it doesn't lock the browser
		retries++;
		if ( retries > 3 ) 
			maxRetriesReached();
		
		if (typeof ng != "undefined" && typeof ng.buzz != "undefined") {
			
			//cleanup
			widgetLibraryLoading = false;
			clearInterval(statusInterval);
			
			//Load the widget
			doWidgetLoad();
		}
	}, 1000);
}