/*
*   MaM E-SHOP - JS Document 
*
*   Author: MaM Multimedia, s.r.o
* 
*/

//Prihlasovanie okno
var _logTime = 0;
$(function() {
	$("#prihlasenie").click(function() {
		if ($("#webLogin:animated").length == 0) {
			if ($("#webLogin").css("display") == "block") {
				LoginSkryt();
				return false;
			}
			
			LoginRamik();
			
			$("#webLoginRamik").stop(true, false).css({display : "block", opacity : 0}).animate({opacity : .65}, 333);
			$("#webLogin").stop(true, false).css({display : "block", opacity : 0}).animate({opacity : 1}, 333, function() {
				$("html").click(function() {
					_logTime = setTimeout(function() { LoginSkryt(); }, 33);
				});
			});
		}
		
		return false;
	});
	
	$("#webLogin").click(function() {
		setTimeout(function() {
			clearTimeout(_logTime);
		}, 0);
	});
});

function LoginSkryt() {
	clearTimeout(_logTime);
	$("html").unbind("click");
	
	$("#webLogin, #webLoginRamik").stop(true, false).animate({opacity : 0}, 333, function() {
		$(this).css({display : "none"});
	});
}

function PX2INT(px) {
	px = ("" + px);
	px = px.substring(0, px.length - 2);
	return parseInt(px, 10);
}

//Vytvorí priesvitný rámik nad prihlasovacím oknom
function LoginRamik() {
	if ($("#webLoginRamik").length == 0) {
		var sirka = 6;

		var d = $("<div>").css({
			background : "url(/images/mainleft.png)",
			position : "absolute",
			color : "white",
			left : "50%",
			top : PX2INT($("#webLogin").css("top")) - sirka,
			width : PX2INT($("#webLogin").css("width")) + sirka * 2,
			height : PX2INT($("#webLogin").css("height")) + sirka * 2,
			marginLeft : PX2INT($("#webLogin").css("marginLeft")) - sirka,
			zIndex : 30,
			borderRadius : 12
		});
		d[0].id = "webLoginRamik";
		$("body").append(d);
	}
}

/* NEW JS FUNCTION */
if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
  }
}


/*  E X T E R N A L   L I N K  */
function external() {
  $("a[rel='external']").attr('target','_blank');
  //shopring links
  $(".partner a").attr('target','_blank');
}


/* T O O L T I P */
var posunY = 10;
var posunX = 20;

function tooltip(){
  $('a[rel=tooltip]').mouseover(function(e) {
    var tip = $(this).attr('title'),
        css = $(this).attr('class');

    $(this).attr('title','');
    $(this).append('<div id="tooltip" class="' + css + '"><div class="tipHeader"></div><div class="tipBody">' + tip + '</div><div class="tipFooter"></div></div>');

    $('#tooltip').css('top', e.pageY + posunY );
    $('#tooltip').css('left', e.pageX + posunX );
    $('#tooltip').css('opacity', 0 );

    //$('#tooltip').fadeIn('500');
    //$('#tooltip').fadeTo('10',0.8);
    $('#tooltip').animate({opacity: 0.8}, {queue:false, duration: 'normal'});

  }).mousemove(function(e) {
    var st  = $(this).hasClass('static');

    if(st) {
      //$('#tooltip').css('top', posunY );
      //$('#tooltip').css('left', posunX );
    }
    else {
      $('#tooltip').css('top', e.pageY + posunY );
      $('#tooltip').css('left', e.pageX + posunX );
    }

  }).mouseout(function() {
    $(this).attr('title',$('.tipBody').html());
    $(this).children('div#tooltip').remove();

  });
}


/*
*   I N I T
*/
$(document).ready(function(){
  tooltip();
  external();
});
