// jQuery Extend myResize
(function($) {
   $.fn.myResize = function() {
        var myImg = $(this);
        function resizeImage() {
        	var imgwidth = myImg.width();
        	var imgheight = myImg.height();
        	var winwidth = $(window).width();
        	var winheight = $(window).height();
        	var widthratio = winwidth / imgwidth;
        	var heightratio = winheight / imgheight;
        	var widthdiff = heightratio * imgwidth;
        	var heightdiff = widthratio * imgheight;
        	if(heightdiff>winheight) {
        		myImg.css({
        			width: winwidth+'px',
        			height: heightdiff+'px'
        		});
        	} else {
        		myImg.css({
        			width: widthdiff+'px',
        			height: winheight+'px'
        		});
        	}
        }
       resizeImage();
       $(window).resize(function() {
           resizeImage();
       });
       return myImg;
   };
})(jQuery);



//jQuery Extend myScreen
(function($) {
    $.fn.myScreen = function() {
        var myScreen = $(this);
        function screenImage() {
        	var screen = $("div.innerScreen", myScreen);
        	for (i=0; i<screen.length; i++) {if (screen.eq(i).css("display")=="block") {break;}}
        	if (i>=(screen.length-1)) {
        		prev = screen.length-1;
        		i = 0;
        		duration=8000;
        	} else {
        		prev = i;
        		i=i+1;
        		if (i==(screen.length-1)) {
        			duration=16000;
        		} else {
        			duration=8000;
        		}
        	}
        	if (screen.length>1) {
	        	screen.eq(i).css("z-index", "1").fadeIn(1200);
	        	screen.eq(prev).css("z-index", "0").fadeOut(600);
	        	interval = setTimeout(function(){screenImage();}, duration);
        	} else {
            	screen.eq(0).css("z-index", "1").fadeIn(1200);
            }
        }
        screenImage();
        return myScreen;
    };
})(jQuery);



//jQuery Start
jQuery(function($) {

   // frameBackGround
   $("img#frameBackGround").css("opacity", "0");



   // screen
   // REPAIR:::: $("div#screen.public div.innerScreen img.innerScreenBack").myResize();
   $('div#screen.public').myScreen();
   $('div#screen.public').click(function() {
	   $(this).animate({opacity: 0}, 1000, function() {
		   $(this).css("z-index","-100");
	   });
   });

   // hover columnNav
   $("div#columnNav ul.level2 li").mouseenter(function(){
	   var pathClass = $(this).attr("class");
	   var exp=/\/eliagilli\/[\S]*.[\S]{3}/;
	   var pathClassFilter = pathClass.match(exp);
	   if (pathClassFilter.length>-1) {
		   var pathToImage = pathClassFilter[0];
		   pathToImage = "http://localhost:8080/author/dms"+pathToImage;
		   if (!$(this).hasClass("active")) {
			   $("img#frameBackGround").dequeue().css("z-index", "99").attr("src", pathToImage).myResize().animate({opacity:1}, {queue: false});
		   }
	   }
   }).mouseleave(function() {
	   $("img#frameBackGround").animate({
		   opacity:0
		   }, {
		   queue: false,
		   complete: function() {
			   //
		   }
		  }).css("z-index", "-99");
   });




   // slider
   $("div#slider", "div#column").each(function(){
	   var slides = $("div.slide", this);
	   $("div#info ul li", "div#column").click(function(){
		   $("div#info ul li", "div#column").not($(this)).removeClass("active");
		   $(this).addClass("active");
		   var num = parseInt($(this).attr("count"))-1;
		   for (i=0; i<slides.length; i++) {
			   if (parseInt(slides.eq(i).css("margin-left"))==0) {
				   if (num!=i) {
					   slides.eq(i).css({"margin-left": "0em", "z-index": "-1"}).animate({"margin-left": "+=40em"}, {duration: 300});
				   }
				break;
			   }
		   }
		   if (num!=i) {
			   slides.eq(num).css({"margin-left": "-40em", "z-index": "1"}).animate({"margin-left": "0em"}, {duration: 280});
		   }
	   });

   });


   // click news image
   $("div.news div.image").click(function(){
	   $("div#newsScreen").css({"z-index":"999", "cursor": "pointer"}).prepend("<div id='screenContent'>"+$(this).html()+"</div>");
   });
   $("div#newsScreen").click(function(){
	  $(this).css({"z-index":"-999", "cursor": "default"});
	  $("div#screenContent").remove();
   });


});

