/*!
 * Telemaque JavaScript Library v0.3
 * http://www.telemaque.fr
 *
 * Copyright (c) 2009 Telemaque
 * Author : Florent Manas
 * Dual licensed under the MIT and GPL licenses.
 * http://www.telemaque.fr/cgv
 *
 * Date: 2009-10-20 10:22:05 -0500 (Wed, 20 Oct 2009)
 * Revision: 355
 */
var hfr_ad_cookie = 'horoscope_fr_home_ad_new';
var flash_version = 0;
var IE6 = false;
var IE7 = false;
var IE8 = false;
var debugging = true;

$(document).ready(function() {
	IE6 = $.browser.msie && $.browser.version.indexOf('6.') != -1;
	IE7 = $.browser.msie && $.browser.version.indexOf('7.') != -1;
	IE8 = $.browser.msie && $.browser.version.indexOf('8.') != -1;

	if (typeof $.fn.pngfix == 'function' && IE6) {
		$('.imgtofix, .bgtofix').pngfix();
		//$('.normal .fun_box td').pngfix({imageFixSrc: '/images/blank.gif', sizingMethod: 'scale'});	
	}
	
	flash_version = deconcept.SWFObjectUtil.getPlayerVersion();
	
	if (typeof onDocumentLoaded == 'function')    onDocumentLoaded();
	if (typeof handleShortLogin == 'function')    handleShortLogin();
	if (typeof handleHeader == 'function')        handleHeader();
	if (typeof handleComments == 'function')      handleComments();
	if (typeof $.fn.replaceAnchors == 'function') $('a[href*="#"]').replaceAnchors();
	if (typeof $.fn.applyRating == 'function')    $('form[class*="rating_form"]').applyRating({});
	if (typeof $.fn.handleMenu == 'function')     $('#navigation a').handleMenu();
	if (typeof $.fn.handleSelect == 'function')   $('.teleSelect').handleSelect();
	if (typeof $.fn.handleFooter == 'function')   $('#footer .main .categories').handleFooter();
	
	//if (typeof $.fn.rotateSigns == 'function')		$('.buttons .sign').rotateSigns();

	
	// IE7 fix buttons
	$('.button').click(function() {
		if ($(this).parent().attr('tagName') == 'A')			
 			document.location.href = $(this).parent().attr('href');
	});
});

var debug = function(string) {
	if (debugging && typeof console != 'undefined' && typeof console.log == 'function'){
		console.log(string);
	}
};

// HOROSCOPES
function viewHoroscopes(options) {

	// 20100610 Modif guilhem -> on fait en sorte de ne plys charger la parti droite de l'horo mais d'aller directement dans la page horo

	$('.buttons .sign').tooltip({top: 2}).click(function(e) {
		$.extend({'title': this.t}, options);
		var id = $(this).attr('id').replace('sign_', '');
		//getHoroscopesBySignId(id, options, this.href);
		
		//e.preventDefault();
	});

	//getHoroscopesBySignId(1, options, $('.buttons .sign').get(0).href);
}


function getHoroscopesBySignId(id, options, href) {
	options = $.extend({'color': '000000', 'bg_color': 'F0D0D3', 'text_limit': 100, 'horoscopes': 'short', 'content': '#horoscope_texts'}, options);

	var text_advanced = options.text_limit == 100 ? ', votre horoscope du ' + options.day_text + ' ' + options.day + ' ' + options.month : '';

	var text ='%%SIGN%%' + text_advanced;

	$(options.content).html('').addClass('loading');

	$.post(
		'/lab/horoscope', 
		{'id': id, 'action': 'get_horoscopes_by_sign_id', 'text_limit': options.text_limit, 'text': text, 'horoscopes': options.horoscopes, 'type': 'html', 'color': options.color, 'href': href}, 
		function(data, textStatus)	{
			if (textStatus == 'success') {
				$(options.content).html(data).removeClass('loading');
				$('form[class="rating_form"]').applyRating({});
			}			
		}
		, 'html'
	);
}

function handleShortLogin() {
	
	
	$(".tag_short_login").click( function() {$(this).find('option').eq(0).hide() ; });
	
	$(".tag_short_login").change(
																																	
		function() {			

			var page_path = [];
			page_path = {'1':'/membre/info' , '2':'/membre/info_modif' , '3':'/membre/identifiant'  , '4':'/membre/credits_historic' , '5':'/membre/credits' ,  '6':'/membre/parrainage'};			
			
			var page_index = $(this).val();							
			var page = page_path[page_index] ;
								
			if(page_index == '0'){return false;}					
			window.location = page;
		}
	);

	
	$("#link_member").hover(
		function () {
						$('#'+this.id + ' div').css("opacity",0.1).animate({ opacity: 0.5}, 500 ) ; 
		}, 
		function () {
						$('#'+this.id + ' div').css("opacity",0.5).animate({ opacity: 1}, 500 ) ;
		}
  );
	
	$('#header_login').keydown(function(event) { if (event.keyCode == 13) { $('#header_login #submit_bt').click(); return false; } });
}


function loadFlash(so, element) {
  if (flash_version['major'] < 8) {
    //$(element).html('<center>Votre navigateur ne possède pas le lecteur Flash ou sa dernière version, pour l\'installer, rendez-vous à la page de téléchargement en cliquant <a href=\"http://www.adobe.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash&Lang=French\" title=\"Télécharger Flash\">ici</a></center>');
		return false;
  } else {
    so.write(element);
		return true;
  } 
}
 
function handleHeader() {
	$('#tools_recherche_input').autocomplete('/lab/ajax/_search_all', {
		width: 250,
		align: 'right',
		max: 4,
		highlight: false,
		scroll: true,
		scrollHeight: 300,
		matchContains: false,
		inputClass: 'header_ac_input',
		resultsClass: 'header_ac_results',
		loadingClass: 'header_ac_loading',
		hoverClass: 'header_ac_hover',
		arrowClass: 'header_ac_arrow',
		oddClass: 'header_ac_odd',
		evenClass: 'header_ac_even',
		/*dataType: 'json',*/ 
		formatItem: function(row, i, max, term) {
			return '<img width="25" src="' + row[1] + '"/><span>' + row[0] + '</span>';
		},
		formatResult: function(row, i) {
			return row[0];
		}
	}).result(function(event, row, formatted) {
		document.location.href = row[2];
	});
	
	$('#blog_submit').click(function() {
		//alert($('#tools_recherche_input').val());			
		$('#tools_recherche_input').search();
	});
}


// TOOLS
// custom easing called "custom" 
$.easing.custom = function (x, t, b, c, d) { 
	var s = 0.80158;  
	if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
	return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b; 
}

$.fn.handleMenu = function() {
	i = 1;
	this.each(function() {
		var current = $(this);
		
		current.attr('i', i);
		i++;
		this.color = current.css('color');
		if (current.hasClass('selected')) {
			current.css({'background-color': this.color, 'color': '#FFFFFF'})
		} else {
			current.hover(
				function() {
					i = parseInt(current.attr('i'));
					prev_i = i - 1;
					//alert('#navigation a[i="' + prev_i + '"]');
					
					$('#navigation a[i="' + prev_i + '"] + div, #navigation a[i="' + i + '"] + div').css('background-color', 'transparent');
					current.css({'backgroundColor': this.color, 'color': '#FFFFFF'});
				},
				function() {
					i = parseInt(current.attr('i'));
					prev_i = i - 1;
					$('#navigation a[i="' + prev_i + '"] + div, #navigation a[i="' + i + '"] + div').css('background-color', '#333333');
					current.css({'backgroundColor': 'transparent', 'color': this.color});
				}
			);
		}

	});
}

$.fn.handleFooter = function() {
	$(this).masonry();
}

//function to get random number upto m
$.random = function(minVal, maxVal, floatVal) {
	floatVal = 0 || floatVal;
  var randVal = minVal + (Math.random() * (maxVal - minVal));
  return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
}

$.fn.replaceAnchors = function() {
  this.each(function(){
		var anchor = $(this);
    if (anchor.hasClass('scroll_to')) {
      anchor.click(function() {				
        var div = anchor.attr('href');
				var y   = parseInt($(div).offset().top + $(div).scrollTop());
				
				options = {axis: 'y', onAfter: function() { document.location.href = div; }};
				if (anchor.has('offset')) {
					$.extend(options, {offset: parseInt(anchor.attr('offset'))});
				}

				//anchor.removeAttr('href');
        $(window).scrollTo({top: y, left: 0}, 500, options);
      });
    }
  });
}

$.fn.handleSelect = function(opt) {
	var opt = $.extend({}, opt);

	$('#horoscope_sign_validation').addClass('hover').click(function() {
		$wrapper = $(this).prev('.teleSelect').children('.select_center');	
	  $(document).attr('location', $('span:eq(0)', $wrapper).attr('index'));																 
	});
	
	var jqTransformHideSelect = function(oTarget){
		var ulVisible = $('.teleSelectBox ul:visible');
		ulVisible.each(function(){
			var oSelect = $(this).parents('.teleSelect:first').find('select').get(0);
			//do not hide if click on the label object associated to the select
			//if( !(oTarget && oSelect.oLabel && oSelect.oLabel.get(0) == oTarget.get(0)) ){$(this).hide();}
			if( !(oTarget) && $(this).parents('.teleSelect:first')) 
				$(this).hide();
		});
	};
	
	this.each(function() {
		teleSelect = $(this);
		sc = $(this).children('.select_center');
		select = sc.children('select');
		
		sc.width(select.width()).css({'z-index': 100});
				
		$(document).mousedown(function(event) {	if ($(event.target).parents('.teleSelectBox').length === 0) { jqTransformHideSelect($(event.target)); } });
		
		select.each(function(index){
			var $select = $(this);

			//var oLabel  =  jqTransformGetLabel($select);
			/* First thing we do is Wrap it */
			var $wrapper = sc;

			$select.remove();

			/* Now add the html for the select */
			$wrapper.prepend('<span></span><ul></ul>');

			var $ul = $('ul', $wrapper).css('width',$select.width()).hide();
			/* Now we add the options */
			$('option', this).each(function(i){
				var oLi = $('<li><a href="#" index="'+ $(this).val() +'">'+ $(this).html() +'</a></li>');
				$ul.append(oLi);
			});

			/* Add click handler to the a */
			$ul.find('a').click(selectOption);

			function selectOption(){
				$('a.selected', $wrapper).removeClass('selected');
				$(this).addClass('selected');	
				/* Fire the onchange event */
				if ($select[0].selectedIndex != $(this).attr('index') && $select[0].onchange) { $select[0].selectedIndex = $(this).attr('index'); $select[0].onchange(); }
				$select[0].selectedIndex = $(this).attr('index');
				if ($select[0].selectedIndex != 0) { $select.trigger('change', opt.change); }
				$('span:eq(0)', $wrapper).html($(this).html()).attr('index', $(this).attr('index'));
				$ul.hide();
				return false;
			}

			/* Set the default */
			$('a:eq('+ this.selectedIndex +')', $ul).click();
			$('span:first', $wrapper).click(function(){$("a.jqTransformSelectOpen",$wrapper).trigger('click');});
			//oLabel && oLabel.click(function(){$("a.jqTransformSelectOpen",$wrapper).trigger('click');});
			//this.oLabel = oLabel;

			/* Apply the click handler to the Open */
			teleSelect
				.click(function(){
					//Check if box is already open to still allow toggle, but close all other selects
					if( $ul.css('display') == 'none' ) { jqTransformHideSelect(); } 
					if($select.attr('disabled')){return false;}

					$ul.slideToggle('fast', function(){					
						var offSet = ($('a.selected', $ul).offset().top - $ul.offset().top);
						$ul.animate({scrollTop: offSet});
					});
					return false;
				})
				.blur(function() { jqTransformHideSelect(); })
			;

			// Set the new width
			var iSelectWidth = $select.outerWidth();
			var oSpan = $('span:first',$wrapper);
			var newWidth = $wrapper.width() + sc.next().width() - sc.prev().width();
			//$wrapper.css('width',newWidth);
			$ul.css('width',newWidth);
			oSpan.css({width:iSelectWidth});
		
			// Calculate the height if necessary, less elements that the default height
			//show the ul to calculate the block, if ul is not displayed li height value is 0
			$ul.css({display:'block',visibility:'hidden'});
			var iSelectHeight = ($('li',$ul).length)*($('li:first',$ul).height());//+1 else bug ff
			(iSelectHeight < $ul.height()) && $ul.css({height:iSelectHeight,'overflow':'hidden'});//hidden else bug with ff
			$ul.css({display:'none',visibility:'visible'});
			
		});
		
	});
}

// TIRAGES
$.fn.handleDrawing = function(type) {
	var type = type || 'marseille';
		
	this.live('click', function() {
		var rand = $.random(1, 22);

		if (this.value == 0) {
			if (drawings.length != $.grep(drawings, function(i) { return i != rand; }).length)
				rand = $.random(1, 22);
				
			this.value = rand;
			drawings.push(rand);
			$(this).attr('src', '/images/blank.gif').addClass('loading').attr('src', '/images/icons/' + type + '/' + this.value + '.jpg').bind('load', function(event) { $(event.target); }).removeClass('loading');
		}
		return false;
	});
}

$.fn.applyRating = function(options) {
	options = $.extend({}, options);

	this.each(function() {
		var form = $(this);

		$(this).show();
		rating = $(this).children('input[name="rating"]').val();
		//$('#rating_form_64_534845').children('input[name="rate"]').rating().rating('select', '4').rating('readOnly', true);
		if ($(this).attr('readonly') != undefined)
			$(this).children('input[name="rate"]').rating(options).rating('select', rating).rating('readOnly', true);
		else
			$(this).children('input[name="rate"]').rating('select', rating).rating(options);
	});
	
	return this;
}

$.fn.rotateSigns = function() {
	if (!$.browser.msie) {
		$(this).each(function() {
			var current = $(this);
			current.data('rotateSign', {'max': randomXToY(4, 8), 'now': 0, 'direction': 1});
			intervalRotateSigns = setInterval(function() { current.rotateSign(); }, randomXToY(20, 50));
		});
		
	}
}

$.fn.rotateSign = function() {
	current = $(this);

	if (Math.abs(current.data('rotateSign').now) >= current.data('rotateSign').max)
		current.data('rotateSign').direction *= -1;

	current.data('rotateSign').now = current.data('rotateSign').now + current.data('rotateSign').direction;

	current.css({'-webkit-transform': 'rotate(' + current.data('rotateSign').now + 'deg)', '-moz-transform': 'rotate(' + current.data('rotateSign').now + 'deg)'}); 
}

$.fn.fokus = function(params) {
	if (!IE6) {
		var params = $.extend({'effect': 'opacity'}, params);
	
		this.hover(
			function(e){	
				current = $(this);
	
				if (params.effect == 'opacity' && !$.browser.msie)
					current.siblings().stop().animate({'opacity': 0.4}, 'fast');
			},
			function(){
				current = $(this);
				
				if (params.effect == 'opacity' && !$.browser.msie)
					current.siblings().stop().animate({'opacity': 1.0}, 'fast');
			}
		);	
	}
	
	return this;
};

$.fn.tooltip = function(params) {
	
	var params = $.extend({'align': 'center', 'valign': 'bottom', 'left': 0, 'top': 0, 'effect': 'opacity', opacity: 0.9}, params);

	if (params.effect == 'focus')
		$(this).focus();

	this.hover(
		function(e){
		  current = $(this);
			
			this.r = randomXToY(5, 15) * -1;

			if (current.hasClass('sign') && !$.browser.msie) {
				current.css({'-webkit-transform': 'rotate(' + this.r + 'deg)', '-moz-transform': 'rotate(' + this.r + 'deg)', 'filter': 'progid:DXImageTransform.Microsoft.BasicImage(rotation=' + this.r + ')'}); 
			}
			
			this.t = current.attr('title');
			current.children().add(current).attr('title', '');
			
			tooltip = $('<span id="tooltip" class="tooltip">'+ this.t +'</span>').appendTo(document.body);
			decal = 0;

			// start
			width = current.width();
			height = current.height();
			
			pt_height = parseInt(current.css('padding-top'));
			pr_width  = parseInt(current.css('padding-right'));
			pb_height = parseInt(current.css('padding-bottom'));
			pl_width  = parseInt(current.css('padding-left'));
																				 
			t_width  = tooltip.width() + ((pl_width + pr_width) / 2);
			t_height = tooltip.height() + ((pt_height + pb_height) / 2);

			if (params.align == 'center')
				x = current.offset().left + (width / 2) - (t_width / 2) + params.left;
			if (params.align == 'left')
				x = current.offset().left + params.left;

			if (params.valign == 'bottom')
				y = current.offset().top + height + pt_height + params.top;
			if (params.valign == 'top')
				y = current.offset().top - t_height + params.top - 10;
			// end

			if (params.effect == 'opacity' && !$.browser.msie)
				current.stop().animate({'opacity': params.opacity}, 'fast');

			if (params.valign == 'top') {
				tooltip.css({'top': y + 'px', 'left': x + 'px', 'display': 'block', 'opacity': 0});
				tooltip.animate({'top': y + 10 + 'px', 'opacity': params.opacity}, 'fast');
			}	else {
				tooltip.css({'top': y + 'px', 'left': x + 'px', 'opacity': params.opacity}).fadeIn('fast');	
			}
    },
		function(){
			current = $(this);
			
			if (current.hasClass('sign') && !$.browser.msie) {
				current.css({'-webkit-transform': 'rotate(0deg)', '-moz-transform': 'rotate(0deg)', 'filter': 'progid:DXImageTransform.Microsoft.BasicImage(rotation=0)'}); 
			}
			
			if (params.effect == 'opacity' && !$.browser.msie)
				current.stop().animate({'opacity': 1}, 'fast');

			current.attr('title', this.t);		
			$('#tooltip').remove();
		}
	);	
	
	return this;
};

$.fn.center = function(params) {
	var options = {
		vertical: true,
		horizontal: true
	}
	
	options = jQuery.extend(options, params);
	
	this.each(function(){
		
		//initializing variables
		var elt = $(this);
		//get the dimensions using dimensions plugin

		var width = elt.width();
		var height = elt.height();
		//get the paddings
		var paddingTop = parseInt(elt.css('padding-top'));
		var paddingBottom = parseInt(elt.css('padding-bottom'));
		//get the borders
		var borderTop = parseInt(elt.css('border-top-width'));
		var borderBottom = parseInt(elt.css('border-bottom-width'));
		//get the media of padding and borders
		var mediaBorder = (borderTop+borderBottom)/2;
		var mediaPadding = (paddingTop+paddingBottom)/2;
		//get the type of positioning
		var positionType = elt.parent().css('position');
		// get the half minus of width and height
		var halfWidth = (width/2)*(-1);
		var halfHeight = ((height/2)*(-1))-mediaPadding-mediaBorder;
		// initializing the css properties
		var cssProp = {
			position: 'absolute'
		};
		
		if(options.vertical) {
			if (height) cssProp.height = height;
			cssProp.top = '50%';
			cssProp.marginTop = halfHeight;
		}
		if(options.horizontal) {
			if (width) cssProp.width = width;
			cssProp.left = '50%';
			cssProp.marginLeft = halfWidth;
		}
		
		//check the current position
		if(positionType == 'static') {
			elt.parent().css('position', 'relative');
		}
		//aplying the css
		elt.css(cssProp);
	
	});
	
	return this;
};

// Classes
(function($) {
	// TeleAutoCompleter
	TeleAutoCompleter = function(options){
		return this.init(options);
	};

	TeleAutoCompleter.prototype = {
		IE: $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent),
		hasFocus: false,
		input: null,
		select: null,
		key: {UP: 38, DOWN: 40, DEL: 46, TAB: 9,	RETURN: 13,	ESC: 27,	COMMA: 188,	PAGEUP: 33,	PAGEDOWN: 34,	BACKSPACE: 8},

		settings: {delay: 5000, data: {}},
		
		init: function(options) {
			this.settings = $.extend(this.settings, options);
			
			this.input = $(this);
			
			
		}
	}

	// TeleBox - ModalBox
	TeleBox = function(opt){
		return this.init(opt);
	};

	TeleBox.prototype = {
		IE: $.browser.msie,
		sizes: {mask: {width: '100%', height: '100%'}, box: {}},
		mask: null,
		box: null,
		cross: null,
		
		init: function(opt) {
			options = opt;

			this.settings  = {delay: 0, margin: 20, width: 'auto', mask_id: 'tele_box_parent', box_id: 'tele_box', close_id: 'tele_box_close', cross_txt: 'Fermer', onInit: function(){}, onLoad: function(){}, onAppend: function() {}, onOpen: function() {},	onClose: function() {}, box_class: 'ui-corner-all'};
			this.mask_css  = {'position': 'fixed', 'left': '0px', 'top': '0px', 'background-color': '#FFFFFF', 'opacity': 0.8, 'display': 'none', 'z-index': 1000};
			this.box_css   = {'position': 'fixed', 'border': 'solid 1px #111111', 'background-color': '#F7F4EF', 'padding': '10px', 'opacity': 1.0, 'font-size': '12px', 'display': 'none', 'z-index': 2000};
			this.cross_css = {'position': 'fixed', 'display': 'none', 'z-index': 3000};
			
			this.loadURL    = false;
			this.hasMask    = true;
			this.isCrossImg = false;
			
			this.mask_parent = null;
			this.box_parent  = null;
			
			this.options = $.extend(this.settings, options);
			this.options.mask_css  = $.extend(this.mask_css, this.options.mask_css || {});
			this.options.box_css   = $.extend(this.box_css, this.options.box_css || {});
			this.options.cross_css = $.extend(this.cross_css, this.options.cross_css || {});
			//console.log(this.options.mask_css);
			if (this.options.loadURL != undefined) this.loadURL = this.options.loadURL;
			if (this.options.hasMask != undefined) this.hasMask = this.options.hasMask;
			if (this.options.isCrossImg != undefined) this.isCrossImg = this.options.isCrossImg;

			this.mask_parent = document.body;			

			if (this.hasMask)
				mask  = '<div id="' + this.options.mask_id + '"></div>';

			box   = '<div id="' + this.options.box_id + '" class="' + this.options.box_class + '"><h3></h3><div class="tele_content"></div></div>';
			//cross = '<div id="' + this.settings.close_id + '" class="submit button hover fuschia"><div class="button_left bgtofix"></div><div class="button_center"><img src="/lab/image?text=Fermer&size=12&color=FFFFFF&bg_color=CF4AA6&font=bauhaus-thin.ttf&min_height=16" /></div><div class="button_right bgtofix"></div></div>';
			
			if (this.isCrossImg)
				cross = '<img class="button hover" id="' + this.options.close_id + '" width="30" height="30" src="/images/icons/close.png">';
			else	
				cross = '<a class="button hover" id="' + this.options.close_id + '"><span class="side fuschia_bt_left"></span><span class="middle fuschia_bt_center">' + this.options.cross_txt + '</span><span class="side fuschia_bt_right"></span></a>';

			if (this.options.box_parent) {
				this.box_parent = $(this.options.box_parent);
				//this.options.mask_css.width  = this.mask_parent.width();
				//this.options.mask_css.height = this.mask_parent.height();
				this.options.onInit();
			}

			//if (this.options.mask_parent) {
			//	this.mask_parent = $(this.mask_parent);
				//this.options.mask_css.width  = this.mask_parent.width();
				//this.options.mask_css.height = this.mask_parent.height();
			//}

			this.box_parent = $(this.options.box_parent || document.body);

			if (this.hasMask) {
				this.mask  = $(mask).css(this.options.mask_css).appendTo(this.mask_parent);
				this.mask.queue('fx');
				this.mask.css({'width': this.sizes.mask.width, 'height': this.sizes.mask.height});
			}
			
			this.box   = $(box).css(this.options.box_css).appendTo(this.box_parent);
			this.cross = $(cross).css(this.options.cross_css).appendTo(this.box_parent);
			
			this.box.queue('fx');
		},
		
		exec: function(elt) {
			current = this;

			$('.tele_content', this.box).html('');

			this.box.css({'width': 'auto', 'height': 'auto'});
			if (elt) {
				if (this.loadURL) {
					$.get(
						elt.href, 
						{}, 
						function(data, textStatus) {
							if (textStatus == 'success') {
								$('.tele_content', current.box).html(data);
								current._show();
							}
						}
						, 'html'
					);	
				} else {
					is_img = false;
					is_embed = false;
					if ($(elt).attr('tagName') == 'A' && $(elt).children('img').length == 1) {
						img = $(elt).children('img').get(0);
						is_img = true;
					}		

					if ($(elt).attr('tagName') == 'IMG') {
						img = elt;
						is_img = true;
					}
					
					if ($(elt).attr('tagName') == 'DIV' && $(elt).children('embed').length == 1) {
						embed = $(elt).children('embed').get(0);
						is_embed = true;
					}				

					if (is_img) {
						if (this.hasMask) this.mask.css({'opacity': 0.5});
					  this.box.css({'background-color': 'transparent', 'border': 'none', 'padding': 0});
						$(img).attr('id', 'tele_img').bind('load', this, this.appear);
						$(elt).appendTo($('.tele_content', this.box));
					} else {
						$('.tele_content', this.box).append(elt);
						if (is_embed) {
							if ($('[src]', embed).length) {
								var so = new SWFObject($(embed).attr('src'), $(elt).attr('id'), '900', '450', '8', 'transparent');
								so.addParam('allowScriptAccess', 'always');
								so.addParam('menu', 'false');
								so.addParam('wmode', 'transparent');
								loadFlash(so, $(elt).attr('id'));
							} else {
								this.options.onAppend();
							}
						}
						this._show();
					}
				}

				this.options.onLoad(this);

				return this;
			}
		},
		
		appear: function(event) {
			if (event && event.data) {
				current = event.data;
			} else {
				return;
			}
			
			current._show();
			
			return this;
		},
			
		disappear: function(event) {
			if (event && event.data) {
				current = event.data;
			} else {
				return;
			}
			
			current._hide();
			
			return this;
		},
		
		update: function() {
			if (this.hasMask) this.mask.css({'width': this.sizes.mask.width, 'height': this.sizes.mask.height});

			padding_x = (parseInt(this.box.css('padding-left')) + parseInt(this.box.css('padding-right')));
			padding_y = (parseInt(this.box.css('padding-top')) + parseInt(this.box.css('padding-top')));

			width  = this.box.width() || this.box.css('width').replace(/px/, '');
			height = this.box.height();
			
			c_width  = this.cross.width(); 
			c_height = this.cross.height() + (parseInt(this.cross.css('padding-top')) + parseInt(this.cross.css('padding-bottom')));

			margin = this.options.margin * 2;

			if (width > (this.box_parent.width() - margin))
				width = this.box_parent.width() - margin - (padding_x / 2);

			if (height > (this.box_parent.height() - margin))
				height = this.box_parent.height() - margin - (padding_y / 2);

			var left = (this.box_parent.width() - (margin / 2) - width) / 2;

			/*
			if (left < this.options.margin) {
			  left = this.options.margin;
				width = this.mask.width() - (this.options.margin * 2);
			}


			debug(parseInt(this.box_parent.offset().top));
			debug(parseInt($(window).scrollTop()) > parseInt(this.box_parent.offset().top));
			*/

			//m_height = this.mask.height();
			//var top  = c_height + 2 + ((m_height - (margin / 2) - height) / 2);
			var top = c_height + 2 + ($(window).scrollTop() > this.box_parent.offset().top ? ($(window).scrollTop() - this.box_parent.offset().top + margin) : margin);
			/* + $(window).scrollTop()
			if (top < this.options.margin) {
			  top = this.options.margin;
				height = this.mask.height() - (this.options.margin * 2);
			}
			*/
			pos = $(this.box_parent).attr('id') == 'body' ? 'fixed' : 'absolute';

			this.box.css({'position': pos, 'left': left + 'px', 'top': top + 'px', 'width': width + 'px', 'height': 'auto'});

			if (this.isCrossImg) {
				c_left = left + width - (30 / 2) + padding_x;
				c_top  = top - (30 / 2);
			} else {
				c_left = left + width - c_width + padding_x + 1;
				//if (this.cross_css.align == 'center')
				//	c_left = left + (width / 2) - (c_width / 2) + (padding_x / 2);
				c_top = top - c_height - 6;
			}
						
			this.cross.css({'position': pos, 'left': c_left + 'px', 'top': c_top + 'px', 'width': c_width + 'px', 'height': c_height + 'px'});
			//console.log('window_width : ' + this.mask.width() + ' - box_width : ' + width + ' - left : ' + this.box.css('left'));
		},
		
		_show: function() {
			if (IE8)
				this.update();
			else
				setTimeout(function(obj) { obj.update(); }, 200, this);
			
			if (this.hasMask) this.mask.bind('click', this, this.disappear).fadeIn('fast');
			
			this.cross.bind('click', this, this.disappear);
			
			if ($.browser.msie) {
				this.box.css({'opacity': 'none', 'display': 'block'});
			} else {
				this.box.delay(200).fadeIn('fast');
			}
			
			this.cross.delay(200).fadeIn('fast');
		},
		
		_hide: function() {			
			if (this.hasMask) this.mask.unbind('click').fadeOut('fast');
			
			if ($.browser.msie) {
				this.box.css({'opacity': 'none', 'display': 'none'});
				this.cross.css({'opacity': 'none', 'display': 'none'});
			} else {
				this.box.fadeOut('fast');
				this.cross.fadeOut('fast');
			}			
			this.options.onClose();
		},
		
		_destroy: function() {
			this.box.add(this.close).remove();
			if (this.hasMask) this.mask.remove();
		}
	}

})(jQuery);

function randomXToY(minVal, maxVal, floatVal) {
	var randVal = minVal + (Math.random() * (maxVal - minVal));
	return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
}

/*
(function($) {

	var TeleBox = function(options) {
		// The jQuery object is actually just the init constructor 'enhanced'
		return new TeleBox.fn.init(options);
	},
	
	IE = $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent);

	TeleBox.fn = TeleBox.prototype = {
		init: function(options) {
			this.options = $.extend(this.settings, options);
			alert(this.options);
			box = $('<div id="' + settings.id + '"<div class="content"><h3></h3><div class="txt"></div></div></div>').appendTo(document.body);
		}
	}
})(jQuery);

var TeleBox = Class.create();
{
	box: null,
	IE: $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent),
	
  settings: {opacity: 0.7, width: 'auto', parent_id: 'tele_box_parent', id: 'tele_box'},
	
  init: function(options) {
		this.options = $.extend(this.settings, options);
		
		box = $('<div id="' + settings.id + '"<div class="content"><h3></h3><div class="txt"></div></div></div>').appendTo(document.body);
	}
});


function tooltipSkySharp() {	
	xOffset = 10;
	yOffset = 20;		

	$('.tooltip').hover(
		function(e){											  
			this.t = $(this).attr('title');
			this.title = '';									  
			$('body').append('<p id="tooltip">'+ this.t +'</p>');
			decal = 0;
			if ((decal = (50 - $('#tooltip').width()) / 2) < 0)
			  decal = 0;
			x = $(this).offset().left + decal;
			y = $(this).offset().top + $(this).height();
			if (!$.browser.msie)
			  $(this).animate({'opacity': 0.8}, 'fast');
			$('#tooltip')
			//.css('top',(e.pageY - xOffset) + 'px')
			//.css('left',(e.pageX + yOffset) + 'px')
				.css('top',(y) + 'px')
				.css('left',(x) + 'px')
			.fadeIn('fast');		
    },
		function(){
			if (!$.browser.msie)
				$(this).animate({'opacity': 1}, 'fast');
			this.title = this.t;		
			$('#tooltip').remove();
		}
	);	
	$('.tooltips').mousemove(function(e){
		$('#tooltip')
			.css('top',(e.pageY - xOffset) + 'px')
			.css('left',(e.pageX + yOffset) + 'px');
	});			
};
*/