﻿(function($) {
	
	$.balloon = {
		
		t: 0,
		timer1: 0,
		min: false,
		// Private methods
		
		_showBalloonAds: function(subject,Html){
			
			//Detected Cookie Browser
			var cookieEnabled=(navigator.cookieEnabled)? true : false;
			//if not IE4+ nor NS6+
			if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
				document.cookie="checkcookie"
				cookieEnabled=(document.cookie.indexOf("checkcookie")!=-1)? true : false 
			}

			if (cookieEnabled){
				if(! $.balloon.Get_Cookie('PopupAds')){
					$("BODY").append(
						'<div id="divBalloonAds">' + 
							'<div class="BalloonAdsRight"></div>' +
							'<div class="BalloonAdsCenter">' +
								'<div class="BalloonAdsTop"></div>' +
								'<div class="BalloonAdsHeader">' +
									'<div class="BalloonAdsTitle">' +
										'<marquee scrolldelay="100" scrollamount="3" id="subject"></marquee>' +
									'</div>' +
									'<div class="BalloonAdsControl">' +
										'<div id="ControlMin" class="balloonAds_control control_min"></div>' +
										'<div id="ControlMax" class="balloonAds_control control_max" style="display:none;"></div>' +
										'<div id="ControlClose" class="balloonAds_control control_close"></div>' +
									'</div>' +
								'</div>' +
								'<div class="BalloonAdsImage">' +
									'<div id="divAdsShow"></div>' +
								'</div>' +
								'<div class="BalloonAdsBottom">' +
								'<div class="BalloonAdsBottomImage"></div>' +
								'</div>' +
							'</div>' +
							'<div class="BalloonAdsLeft"></div>' +
						'</div>'
					  );
					// IE6 Fix
					//var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed'; 
					
					if ($.browser.msie && parseInt($.browser.version) <= 6 ){
						$("#divBalloonAds").css({
							width:'254px', 
							height:'227px', 
							overflow:'hidden', 
							bottom:'0', 
							right:'0',
							zIndex:'67068', 
							position:'absolute', 
							display:'block'
						});
						jInitPopup();
					}
					else{
						$("#divBalloonAds").css({
							width:'254px', 
							height:'227px', 
							overflow:'hidden', 
							bottom:'0', 
							right:'0', 
							zIndex:'67068', 
							position:'fixed', 
							display:'block'
						});
					}
					
					$("#subject").text(subject);
					$("#divAdsShow").html(Html);
					
					$.balloon._showLK247BalloonAds();
					
					$("#ControlMin").click( function() {
						$.balloon._minLK247BalloonAds();
					});
					
					$("#ControlMax").click( function() {
						$.balloon._maxLK247BalloonAds();
					});
					
					$("#ControlClose").click( function() {
						$.balloon._closeLK247BalloonAds();
					});
				}
			}
		},
		
		_showLK247BalloonAds: function() {	
			var sPos = 'bottom_right';
			if (sPos == 'bottom_left')
			{
				document.getElementById("divBalloonAds").style.left = 0;
				document.getElementById("divBalloonAds").style.display = 'block';		
			}
			else if (sPos == 'bottom_right')
			{
				document.getElementById("divBalloonAds").style.right = 0;
				document.getElementById("divBalloonAds").style.display = 'block';		
			}
			$.balloon.t = setTimeout("jMinLK247BalloonAds()", 20000);
		},
		
		_minLK247BalloonAds: function() {
			
			if ($.browser.msie && parseInt($.browser.version) <= 6 ){
				$.balloon.min = true;
			}
			$("#ControlMax").show();
			$("#ControlMin").hide();
			$("#divBalloonAds").animate({height:"20px"},700);
		},
		
		_maxLK247BalloonAds: function() {
			if ($.browser.msie && parseInt($.browser.version) <= 6 ){
				$.balloon.min = false;
			}
			$("#ControlMax").hide();
			$("#ControlMin").show();	
			$("#divBalloonAds").animate({height:"227px"},700);
			$('#divAdsShow').show();
		},
		
		_closeLK247BalloonAds: function() {
			$("#divBalloonAds").fadeOut("slow");
			//setTimeout("_showLK247BalloonAds()", 900000);	
			//Write Cookie
			$.balloon.Set_Cookie('PopupAds','PopupAds',15,'/','','');
		},
		
		_onRunPlayer: function() {
			$('#msg').hide();
		},
		
		_setTimeDelay: function(){
			$.balloon.t = setTimeout("jMinLK247BalloonAds()", 20000);
		},
		
		_clearTimeDelay: function(){
			clearTimeout($.balloon.t);
		},
		_getScrollTop: function () {
            var scrOfY = 0;
            if (typeof (window.pageYOffset) == 'number') {
                //Netscape
                scrOfY = window.pageYOffset;
            }
            else if (document.body && (document.body.scrollTop)) {
                //DOM
                scrOfY = document.body.scrollTop;
            }
            else if (document.documentElement && (document.documentElement.scrollTop)) {
                //DOM
                scrOfY = document.documentElement.scrollTop;
            }
            return scrOfY;
        },

        _getSizeBrowser: function (size) {
            var mySize = 0;
            if (typeof (window.innerWidth) == 'number') {
                //Non-IE
                if (size == 'width') {
                    mySize = window.innerWidth;
                }
                else {
                    mySize = window.innerHeight;
                }
            }
            else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
                //IE 6+
                if (size == 'width') {
                    mySize = document.documentElement.clientWidth;
                }
                else {
                    mySize = document.documentElement.clientHeight;
                }
            }
            else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
                //IE 4
                if (size == 'width') {
                    mySize = document.body.clientWidth;
                }
                else {
                    mySize = document.body.clientHeight;
                }
            }
            return mySize;
        },
		
		_InitPopup: function InitPopup() {
			var _top = $.balloon._getSizeBrowser('height') + $.balloon._getScrollTop() - 227;
			
			if ($.balloon.min == true){
				document.getElementById('divBalloonAds').style.top =  _top + 206 + "px";
				document.getElementById('divBalloonAds').style.bottom = '0';
				document.getElementById('divBalloonAds').style.right = '0';
			}
			else{
				document.getElementById('divBalloonAds').style.top =  _top + "px";
				document.getElementById('divBalloonAds').style.bottom = '0';
				document.getElementById('divBalloonAds').style.right = '0';
			}
			$.balloon.timer1 = setTimeout("jInitPopup()", 0.000001);
        },
		
		Set_Cookie: function (name, value, expires, path, domain, secure) {
			// set time, it's in milliseconds
			var today = new Date();
			today.setTime(today.getTime());

			/*
			if the expires variable is set, make the correct
			expires time, the current script below will set
			it for x number of days, to make it for hours,
			delete * 24, for minutes, delete * 60 * 24
			*/
			if (expires) {
				//expires = expires * 1000 * 60 * 60 * 24;
				expires = expires * 1000 * 60 ;
			}
			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" : "");
		},

		Get_CookieValue: function (check_name) {
			// first we'll split this cookie up into name/value pairs
			// note: document.cookie only returns name=value, not the other components
			var a_all_cookies = document.cookie.split(';');
			var a_temp_cookie = '';
			var cookie_name = '';
			var cookie_value = '';
			var b_cookie_found = false; // set boolean t/f default f

			for (i = 0; i < a_all_cookies.length; i++) {
				// now we'll split apart each name=value pair
				a_temp_cookie = a_all_cookies[i].split('=');


				// and trim left/right whitespace while we're at it
				cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

				// if the extracted name matches passed check_name
				if (cookie_name == check_name) {
					b_cookie_found = true;
					// we need to handle case where cookie has no value but exists (no = sign, that is):
					if (a_temp_cookie.length > 1) {
						cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
					}
					// note that in cases where cookie is initialized but no value, null is returned
					return cookie_value;
					break;
				}
				a_temp_cookie = null;
				cookie_name = '';
			}
			if (!b_cookie_found) {
				return null;
			}
		},

		Delete_Cookie: function (name, path, domain) {
			if (Get_Cookie(name)) document.cookie = name + "=" +
		((path) ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "") +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		},

		Get_Cookie: 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));
		}
		
	}
	
	// Shortuct functions
	
	
	jShowBalloon = function(title, sHTML) {
		$.balloon._showBalloonAds(title, sHTML);
	}
	
	jSetDeplay = function (){
		$.balloon._setTimeDelay();
	}
	
	jClearDeplay = function () {
		$.balloon._clearTimeDelay();
	}
	
	jMinLK247BalloonAds = function(){
		$.balloon._minLK247BalloonAds();
	}
	
	jInitPopup = function(){
		$.balloon._InitPopup();
	}
	
})(jQuery);
