/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, domain, secure].join('') + '; path=/';
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

var support_widget = function()
{
	var counter;
	var isShown=false;
	var aClicks, aSeconds, expTime, widgetId;
	this.defaultOptions = {
			'aClicks': 10,
			'aSeconds': 180
			
	};
	return {
		getBrowserLanguage: function()
		{
		
		    var lang;
		    if (typeof navigator.userLanguage != "undefined") {
		        lang = navigator.userLanguage.toUpperCase();
		    } else if (typeof navigator.language != "undefined") {
		        lang = navigator.language.toUpperCase();
		    }
		    alert(lang);
		  
		},
		appendWidget: function()
		{
			jQuery('body').append("<div id=\"supportWidget\" style=\"display: none\" >" + 
				"<img id=\"support_widget_close_image\" src=\"httP://www.goobercall.com/images/close_sw.png\" style=\"width:24px;height:24px\" width=\"24\" height=\"24\"/>"+
				"<object width=\"318\" height=\"204\" align=\"middle\" style=\"display: none\" id=\"widgetIE\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\">" + 
				"<param value=\"sameDomain\" name=\"allowScriptAccess\"/>" + 
				"<param value=\"false\" name=\"allowFullScreen\"/>" + 
				"<param value=\"http://www.goober.com/widget/regular/widget.swf?wid=" + widgetId + "\" name=\"movie\"/>" + 
				"<param value=\"high\" name=\"quality\"/>" + 
				"<param value=\"#333333\" name=\"bgcolor\"/>" + 
				"<embed width=\"318\" height=\"204\" align=\"middle\" id=\"widgetFF\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" allowfullscreen=\"false\" allowscriptaccess=\"sameDomain\" name=\"widget\" bgcolor=\"#333333\" quality=\"high\" src=\"http://www.goober.com/widget/regular/widget.swf?wid=" + widgetId + "\" />" + 
				"</object>" + 
				"</div>");
			jQuery('img#support_widget_close_image').bind('click', function(){
				support_widget.loadSupportPanel();
			});
			
		},
		appendSupportPanel: function()
		{
			jQuery('body').append("<div id=\"supportWidgetFolded\">" +
					"<img id=\"support_widget_help_image\" src=\"../../../../images/ring.png\"/>" + '<span>Support</span>' + 
					"<img id=\"support_widget_open_image\" src=\"../../../../images/open_sw.png\"/>" + 
					"</div>");
			jQuery('img#support_widget_open_image').bind('click', this.loadWidget);
			
		},
		loadWidget: function()
		{
			var self = this;
			if(jQuery('#supportWidgetFolded').length==1)
			{
				jQuery('#supportWidgetFolded').hide('fast', function(){
					jQuery(this).remove();
					support_widget.appendWidget();
				});
				
			}
			else{
				this.appendWidget();
			}
			window.setTimeout(function(){
				jQuery('#supportWidget').slideDown('fast', function(){
				    jQuery('#widgetIE').show('slow');
				  });
				jQuery.cookie('isShown', true);
				
				
			}, 1000);
			
		},
		loadSupportPanel: function()
		{
			if(jQuery('#supportWidget').length==1)
			{
			      jQuery('#widgetIE').remove();
				jQuery('#supportWidget').slideUp('fast', function(){
					jQuery(this).remove();
					support_widget.appendSupportPanel();
				});	
			}
			else{
				this.appendSupportPanel();
			}
			
			
		},
		readCookie: function()
		{
			if(jQuery.cookie('isShown'))
			{
				this.loadSupportPanel();
			}
			else{
			counter = jQuery.cookie('counter');
		    if(counter && counter >= aClicks)	    	
		    {
		     this.loadWidget(widgetId);      
		    }
		    else{
		    	expTime = jQuery.cookie('expTime');
		    	var actTime = new Date();
		    	var self = this;
		    	if(expTime && expTime > actTime.getTime())
		    	{
		    		window.setTimeout(function(){self.loadWidget();}, expTime-actTime.getTime());
		    	}
		    		
		    }
			}
		},
		setCookie: function()
		{
		    counter = jQuery.cookie('counter')?jQuery.cookie('counter'):0;
		    counter++;
		    jQuery.cookie('counter', counter);
		    if(counter >= aClicks && !jQuery.cookie('isShown'))
		    {
		    	this.loadWidget();
		    }
		},
		initSession: function(options)
		{
			var self = this;
		  if(!jQuery.cookie('expTime')) // first call
		  {
			  
			  
			  myDate = new Date();
			  expTime = myDate.getTime() + aSeconds*1000;
			  jQuery.cookie('expTime', expTime );
		  }
		  widgetId=options.widgetId;
		  aClicks=typeof options.aClicks=='undefined'?defaultOptions.aClicks : options.aClicks;
		  aSeconds=typeof options.aSeconds=='undefined'?defaultOptions.aSeconds: options.aSeconds;
		  jQuery('a').bind('click', function(){
			  self.setCookie();
			  return true;
		  });
		  //this.getBrowserLanguage();
		  this.readCookie();
		}
	};
}();

