
//Reusable pop-up layer box
$(document).ready( function() {
	$("a.ciba_popup").cibadialog({width:390});
	$('.backtotopdiv').click(function(){$('#masthead').ScrollTo(800);return false});
	$("#aside a").click( function() {
		var sId = $(this).attr('class');
		if( $("#"+sId).prev(".backtotopcontainerdiv").length )
			sTo = $("#"+sId).prev(".backtotopcontainerdiv");
		else
			sTo= $("#"+sId).prev().prev(".backtotopcontainerdiv");
		$(sTo).ScrollTo(800);
		return false;
	});
	
	//start
	$("#lib").click( function(){

		openDivCenter("tv-ads-overlay","closeBtnLink");
		var vars = { } 
		var params = {
			  scale: "noScale", 
			  salign: "", 
			  menu: "true", 
			  allowFullScreen: "true", 
			  scale: "showall", 
			  wmode: "transparent"
			};
		var attributes = { 
			  id: "swfTVSpot", 
			  name:"swfTVSpot" 
		};
		
		
		var strVar="";
		strVar += "<object type=\"application\/x-shockwave-flash\" width=\"665\" height=\"370\"";
		strVar += "		wmode=\"transparent\" data=\"\/swf\/flvplayer.swf?file=\/swf\/LIB_30_AirOptixAQUA_1.flv&autoStart=true\">";
		strVar += "		<param name=\"movie\" value=\"\/swf\/flvplayer.swf?file=\/swf\/LIB_30_AirOptixAQUA_1.flv&autoStart=true\" \/>";
		strVar += "		<param name=\"wmode\" value=\"transparent\" \/>";
		strVar += "		<\/object>";

		document.getElementById("flashcontent").innerHTML =strVar;
		return false;
	});

	$("#jog").click( function(){

		openDivCenter("tv-ads-overlay","closeBtnLink");
		var vars = { } 
		var params = {
			  scale: "noScale", 
			  salign: "", 
			  menu: "true", 
			  allowFullScreen: "true", 
			  scale: "showall", 
			  wmode: "transparent"
			};
		var attributes = { 
			  id: "swfTVSpot", 
			  name:"swfTVSpot" 
		};
		
		
		var strVar="";
		strVar += "<object type=\"application\/x-shockwave-flash\" width=\"665\" height=\"370\"";
		strVar += "		wmode=\"transparent\" data=\"\/swf\/flvplayer.swf?file=\/swf\/JOG_30_V1.flv&autoStart=true\">";
		strVar += "		<param name=\"movie\" value=\"\/swf\/flvplayer.swf?file=\/swf\/JOG_30_V1.flv&autoStart=true\" \/>";
		strVar += "		<param name=\"wmode\" value=\"transparent\" \/>";
		strVar += "		<\/object>";

		document.getElementById("flashcontent").innerHTML =strVar;
		return false;
	});
	
	$("#popup-lense").click( function(){

		openDivCenter("tv-ads-overlay","closeBtnLink");
		var vars = { } 
		var params = {
			  scale: "noScale", 
			  salign: "", 
			  menu: "true", 
			  allowFullScreen: "true", 
			  scale: "showall", 
			  wmode: "transparent"
			};
		var attributes = { 
			  id: "swfTVSpot", 
			  name:"swfTVSpot" 
		};
		
		
		var strVar="";
		strVar += "<object type=\"application\/x-shockwave-flash\" width=\"665\" height=\"470\"";
		strVar += "		wmode=\"transparent\" data=\"\/swf\/flvplayer.swf?file=\/swf\/Teens-Eyedoll-video.flv&autoStart=true\">";
		strVar += "		<param name=\"movie\" value=\"\/swf\/flvplayer.swf?file=\/swf\/Teens-Eyedoll-video.flv.flv&autoStart=true\" \/>";
		strVar += "		<param name=\"wmode\" value=\"transparent\" \/>";
		strVar += "		<\/object>";

		document.getElementById("flashcontent").innerHTML =strVar;
		return false;
	});
	
});


//-------------------------------------------------------------------------------------------
// POSITION DIV IN THE CENTER OF WINDOW
//------------------------------------------------------------------------------------------
function centerLyr(divID)
{
    $('#'+divID).css("position","absolute");
 
    var newTop = ( $(window).height() - $('#'+divID).height() ) / 2+$(window).scrollTop();
    if(newTop<0) newTop = 0;

    var newLeft = ( $(window).width() - $('#'+divID).width() ) / 2+$(window).scrollLeft();
    if(newLeft<0) newLeft = 0;

    $('#'+divID).css("top", newTop + "px");
    $('#'+divID).css("left", newLeft + "px");
}

//-------------------------------------------------------------------------------------------
// RE-POSITION DIV IN THE CENTER OF WINDOW ON WINDOW RESIZE
//------------------------------------------------------------------------------------------
function rePositionLyr(status,divID) 
{
    switch(status) {
	case true:
		$(window).bind('resize', function() {
			centerLyr(divID);
		});
		//scroll is not used here
		//$(window).bind('scroll', function() {
		//	centerLyr(divID);
		//});
	    break;
	case false:
		$(window).unbind('resize');
		//scroll is not used here
		//$(window).unbind('scroll');
	    break;
    }
}

//-------------------------------------------------------------------------------------------
// POSITION OVERLAY IN THE CENTER OF WINDOW
//------------------------------------------------------------------------------------------
function showPopupDivCenter(divID) 
{
   if($.browser.msie && $.browser.version == '6.0') $('#'+divID).bgiframe();
    var toShow = $("#"+divID); 
    toShow.css({"z-index":"9000","visibility":"visible","display":"block"});
    centerLyr(divID);
    rePositionLyr(true,divID);
}

//-------------------------------------------------------------------------------------------
// OPEN DIV OVERLAY AND POSITION CENTER
//------------------------------------------------------------------------------------------
function openDivCenter(divID,closeBtnID)
{
    var toShow="";
    // Build the background overlay div
    toShow += "<div class='site_overlay'></div>";
    $('body').append(toShow);

    $('div.site_overlay').css('height',$(document).height()).bind('click',function(){
	closeDiv(divID);
	return false;
    });

    $('div.site_overlay').css('opacity',0).fadeTo('slow',0.65, function(){
	showPopupDivCenter(divID);
	$('div.site_overlay').css("z-index","9000");
	$('#'+divID).css("z-index","9500");
    });

    if(closeBtnID!=""){
        $("#"+closeBtnID).bind('click',function(){
	        closeDiv(divID);
		return false;
        });
    }
}

//-------------------------------------------------------------------------------------------
// CLOSE OPENED DIV OVERLAY
//------------------------------------------------------------------------------------------
function closeDiv(divID)
{
    $('div.site_overlay').remove();
    rePositionLyr(false,divID);
    var toHide = $("#"+divID); 
    toHide.css({"z-index":"0","visibility":"hidden"});
    if(divID=="all-products"){
	toHide.css("width", "1px");
	toHide.css("height", "1px");
    }
    if(divID=="tv-ads-overlay")
    {
		$('#swfTVSpot').remove();
		$('#flashcontent').remove();
		$("#tv-ads-overlay").children("div.lyrRel").append("<div id='flashcontent'></div>");
    }	
}


(function($){
	$.fn.cibadialog = function(options) {
		var $dropShadow, $cibaDialog, $cibaContent;
		
		var defaults = {
			width: 400,
			height: 200,
			position: 'center',
			cibaZindex : 10 
		};
		
		var options = $.extend(defaults, options);
		var cibaZindex = options.cibaZindex;
		
		return this.each( function() {
			
			var $this = $(this);
			
			if( !$("#ciba-widget").length) {
				$(['<div id="ciba-widget">',
					'<div id="ciba-dialog">',
//						'<div class="top">',
							'<h3 class="page_title"></h3>',
//							'<div class="lt"><h3 class="page_title" style="border:1px solid red;"></h3></div>',
							'<div style="position:absolute; right:4px;top:4px;_top:12px;_right:12px;cursor:pointer;" class="ciba-close close"><img src="/images/ico_close.png" alt="Close" title="Close"/></div>',
//							'<div class="clrboth"></div>',
//						'</div>',
						'<div class="content">',
						'</div>',
					'</div>',
				'</div>'].join('')).appendTo('body');
				
				$(".ciba-close").bind('click', function(){
					$("#ciba-widget").fadeOut('slow');
					return false;
				});
			}
			
			var $cibaWidget = $("#ciba-widget").css({width:options.width, zIndex:cibaZindex});
			$cibaDialog = $("#ciba-dialog").css({zIndex:cibaZindex});
			
			if (!$dropShadow) {
				$dropShadow = $([]);
				for( var i=0;i<7; i++ ) {
					$dropShadow = $dropShadow.add($('<div></div>').css({zIndex: cibaZindex-1, opacity:.1, top: 1+i, left: 1+i}));
				}
				$dropShadow.css({position:'absolute', backgroundColor:'#000', width:options.width}).attr("class","shdw");
				$dropShadow.prependTo($cibaWidget);
			}
			
			$cibaContent = $("#ciba-dialog .content");
			 
			$this.bind('click', function(event){
				var page = $this.attr('href');
				if( !$cibaWidget.is(':hidden')) {
					$cibaWidget.hide();
				}
				$.ajax({
					url: page,
					context: $cibaContent,
					success : function(data, textStatus) {
						$cibaContent.html(data);
						var title = $cibaContent.find('h3').text();
						$("#ciba-widget .page_title").html(title);
						$("#ciba-dialog .content h3").remove();
						$cibaWidget.css({top:event.pageY + 15,left:event.pageX}).fadeIn('fast');
						var cHeight = $cibaDialog.height();
						$dropShadow.css({height:cHeight+35});
					}
				});
				return false;
			});
			
		});
	}
})(jQuery);		

//////////////////// scroll ////////////////////////////////////////////////////////////////////////////
jQuery.getPos = function (e)
{
	var l = 0;
	var t  = 0;
	var w = jQuery.intval(jQuery.css(e,'width'));
	var h = jQuery.intval(jQuery.css(e,'height'));
	var wb = e.offsetWidth;
	var hb = e.offsetHeight;
	while (e.offsetParent){
		l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
		t += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
		e = e.offsetParent;
	}
	l += e.offsetLeft + (e.currentStyle?jQuery.intval(e.currentStyle.borderLeftWidth):0);
	t  += e.offsetTop  + (e.currentStyle?jQuery.intval(e.currentStyle.borderTopWidth):0);
	return {x:l, y:t, w:w, h:h, wb:wb, hb:hb};
};
jQuery.getClient = function(e)
{
	if (e) {
		w = e.clientWidth;
		h = e.clientHeight;
	} else {
		w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;
		h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;
	}
	return {w:w,h:h};
};
jQuery.getScroll = function (e) 
{
	if (e) {
		t = e.scrollTop;
		l = e.scrollLeft;
		w = e.scrollWidth;
		h = e.scrollHeight;
	} else  {
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
			l = document.documentElement.scrollLeft;
			w = document.documentElement.scrollWidth;
			h = document.documentElement.scrollHeight;
		} else if (document.body) {
			t = document.body.scrollTop;
			l = document.body.scrollLeft;
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
	}
	return { t: t, l: l, w: w, h: h };
};

jQuery.intval = function (v)
{
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};

jQuery.fn.ScrollTo = function(s) {
	o = jQuery.speed(s);
	return this.each(function(){
		new jQuery.fx.ScrollTo(this, o);
	});
};

jQuery.fx.ScrollTo = function (e, o)
{
	var z = this;
	z.o = o;
	z.e = e;
	z.p = jQuery.getPos(e);
	z.s = jQuery.getScroll();
	z.clear = function(){clearInterval(z.timer);z.timer=null};
	z.t=(new Date).getTime();
	z.step = function(){
		var t = (new Date).getTime();
		var p = (t - z.t) / z.o.duration;
		if (t >= z.o.duration+z.t) {
			z.clear();
			setTimeout(function(){z.scroll(z.p.y, z.p.x)},13);
		} else {
			st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
			sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
			z.scroll(st, sl);
		}
	};
	z.scroll = function (t, l){window.scrollTo(l, t)};
	z.timer=setInterval(function(){z.step();},13);
};

