(function($) {

$.fn.hw_awesomebox = function() {

var pictures = new Array();
var current_img = 0;
var current_scroll = 0;
var current_height = 300;
var current_width = 400;

preload();

$(this).click(function(){
         awesome(this);
         return false;
});
function awesome(obj_clicked){	 
         make_divs();
         get_gallerie(obj_clicked);
         prepare_img(current_img);
}
function preload(){
        var pics = Array();
        pics[0] = 'ico-loading.gif';
        pics[1]	= 'top_left.png'
        pics[2] = 'top_right.png';
        pics[3] = 'bot_left.png';
        pics[4] = 'bot_right.png';
        pics[5] = 'close.png';
        pics[6] = 'close_h.png';
        pics[7] = 'arrow_l.png';
        pics[8] = 'arrow_r.png';
        pics[9] = 'arrow_l_h.png';
        pics[10] = 'arrow_r_h.png';
	for(i=0;i<pics.length;i++){
                 image = new Image();                 
                 image.src = './hw_awesomebox/'+pics[i];
         }	
}
function get_gallerie(obj_clicked){
         var rel = $(obj_clicked).attr('rel');
         if (typeof rel !== 'undefined' && rel !== false && rel !== '') {
                 var count = 0;
                 $('a.hw_awesomebox').each(function(){
                         if($(this).attr('rel') == rel){
                                 pictures[count] = new Array(3);
                                 pictures[count]['src'] = $(this).attr('href');
                                 pictures[count]['alt'] = $(this).children('img').attr('alt');
                                 pictures[count]['title'] = $(this).children('img').attr('title');
                                 if(this == obj_clicked){
                                         current_img=count;
                                 }
                                 count++;
                         }
                 });
         }else{
                 pictures[0] = new Array(3);
                 pictures[0]['src'] = $(obj_clicked).attr('href');
                 pictures[0]['alt'] = $(obj_clicked).children('img').attr('alt');
                 pictures[0]['title'] = $(obj_clicked).children('img').attr('title');
                 $('#hw_awesomebox-arrows').hide();
         }

}
function prepare_img(number){
         var pic = new Image();
         $('#hw_awesomebox-img-container').hide();
         $('#hw_awesomebox-img-info').children().remove();
         $('#hw_awesomebox-img-info').hide();
         pic.onload = function(){
                 $('#hw_awesomebox-img').attr('src', pic.src); //Reihenfolge wichtig
                 var measurments = get_measurments(pic.width,pic.height); // da sonst bild vorher angezeigt wird evt wegen animate
                 resize(measurments['width'],measurments['height'],measurments['top'],measurments['max_height'],measurments['left']);
                 pic.onload=function(){}; // sth for IE
          };
          pic.src = pictures[number]['src'];
}
function show_pic(){
        $('#hw_awesomebox-img-container').show();
}
function end(){
         current_img = 0;
         $('#hw_awesomebox-overlay').fadeOut('slow',function(){ $(this).remove(); });
         $('#hw_awesomebox-overlay').remove();         
         $('#hw_awesomebox-border-container').remove();
}
function make_divs(){
         /*
         <div id="hw_awesomebox-overlay"></div>
         <div id="hw_awesomebox-border-container">         	 
                 <div id="hw_awesomebox-top-left"></div>
                 <div id="hw_awesomebox-top-mid"></div>
                 <div id="hw_awesomebox-top-right"></div>
                 <div id="hw_awesomebox-right"></div>
                 <div id="hw_awesomebox-bot-right"></div>
                 <div id="hw_awesomebox-bot-mid"></div>
                 <div id="hw_awesomebox-bot-left"></div>
                 <div id="hw_awesomebox-left"></div>
                 <div id="hw_awesomebox-loading">                 
                 	<div id="hw_awesomebox-img-container">
                         	<div id="hw_awesomebox-arrows">
                                      	<img id="hw_awesomebox-arrow-left" class="fleft needpngfix" src="/frontend/module/hw_awesomebox/arrow_left.jpg"/>
	                                <img id="hw_awesomebox-arrow-right" class="needpngfix" src="/frontend/module/hw_awesomebox/arrow_right.jpg"/>
                                 </div>
	                         <div id="hw_awesomebox-close">
	                                 <img id="hw_awesomebox-button-close" class="needpngfix" src="/frontend/module/hw_awesomebox/close.jpg" />
                                 </div>
	                         <div id="hw_awesomebox-img-box">
	                         	<img src="" id="hw_awesomebox-img"/></div><div id="hw_awesomebox-img-info">
                         	</div>
                         </div>
                 	<div id="hw_awesomebox-img-info"></div>
                 	</div>
                 </div>
         </div>
         */
         $('body').append('<div id="hw_awesomebox-overlay"></div><div id="hw_awesomebox-border-container"><div id="hw_awesomebox-top-left"></div><div id="hw_awesomebox-top-mid"></div><div id="hw_awesomebox-top-right"></div><div id="hw_awesomebox-right"></div><div id="hw_awesomebox-bot-right"></div><div id="hw_awesomebox-bot-mid"></div><div id="hw_awesomebox-bot-left"></div><div id="hw_awesomebox-left"></div><div id="hw_awesomebox-outer-img-container"><div id="hw_awesomebox-loading"><div id="hw_awesomebox-img-container"><div id="hw_awesomebox-arrows"><img id="hw_awesomebox-arrow-left" class="fleft needpngfix" src="/frontend/module/hw_awesomebox/arrow_l.png"/><img class="needpngfix" id="hw_awesomebox-arrow-right" src="/frontend/module/hw_awesomebox/arrow_r.png"/></div><div id="hw_awesomebox-close"><img id="hw_awesomebox-button-close" class="needpngfix" src="/frontend/module/hw_awesomebox/close.png" /></div><div id="hw_awesomebox-img-box"><img src="" id="hw_awesomebox-img"/></div></div><div id="hw_awesomebox-img-info"></div></div></div>');
         /* ie 6 png fix */
         var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

         if (jQuery.browser.msie && ie6) {         	
         	$('img.needpngfix').ifixpng();
         	$('#hw_awesomebox-top-right,#hw_awesomebox-top-left,#hw_awesomebox-bot-left,#hw_awesomebox-bot-right').ifixpng();         	
         }
         //---------------------------------
         $('#hw_awesomebox-overlay').css('opacity',0.7); // becaus css wont do it right in IE
         $('#hw_awesomebox-close').css('opacity',0.2);
         $('#hw_awesomebox-arrows').css('opacity',0.2);
         $('#hw_awesomebox-img-info').css('opacity',0.2);
         //container first time
         $('#hw_awesomebox-border-container').css('height',current_height);
         $('#hw_awesomebox-border-container').css('width',current_width);
         var page_size = ___getPageSize();
         var scroll = ___getPageScroll();         
         $('#hw_awesomebox-border-container').css('top',((page_size[3]-current_height)/2)-20+scroll[1]);
         $('#hw_awesomebox-border-container').css('left',(page_size[2]-current_width)/2)-20;
         //add - events
         $('#hw_awesomebox-overlay').click(function(){
                 end();
         });

         $('#hw_awesomebox-arrow-left').click(function(){
                 current_img -= 1;
                 if(current_img == -1){
                         current_img=pictures.length-1;
                 }
                 prepare_img(current_img);
         });
         $('#hw_awesomebox-arrow-right').click(function(){
                 current_img += 1;
                 if(current_img == pictures.length){
                         current_img = 0;
                 }
                 prepare_img(current_img);
         });
         $('#hw_awesomebox-button-close').click(function(){ end();});

         $('#hw_awesomebox-arrow-right,#hw_awesomebox-arrow-left,#hw_awesomebox-button-close').mouseenter(function(e){
                 var item = $(e.target);
		 var src = $(item).attr('src');
		 $(item).attr('src',src.substr(0,src.length - 4)+ '_h.png');                 
         });
         $('#hw_awesomebox-arrow-right,#hw_awesomebox-arrow-left,#hw_awesomebox-button-close').mouseleave(function(e){
	         var item = $(e.target);
		 var src = $(item).attr('src');
		 $(item).attr('src',src.substr(0,src.length - 6)+ '.png');		                 
         });
         $('#hw_awesomebox-border-container').mouseover(function(){
                 $('#hw_awesomebox-arrows').css('opacity',0.6);
                 $('#hw_awesomebox-close').css('opacity',0.6);
                 $('#hw_awesomebox-img-info').css('opacity' , 0.6);
         }).mouseleave(function(){
                 $('#hw_awesomebox-arrows').css('opacity',0.2);
                 $('#hw_awesomebox-close').css('opacity',0.2);
                 $('#hw_awesomebox-img-info').css('opacity',0.2);                 
         });

        $(window).scroll(function()
         {         	 
                 scroll = ___getPageScroll();
                 if(scroll[1] > current_scroll){                 	 
                         $('#hw_awesomebox-overlay').animate({'top':'+='+(scroll[1]-current_scroll)},100);
                         $('#hw_awesomebox-border-container').animate({'top':'+='+(scroll[1]-current_scroll)},100);
                         current_scroll = scroll[1];
                 } else{
                         $('#hw_awesomebox-overlay').animate({'top':'-='+(current_scroll-scroll[1])},100);
                         $('#hw_awesomebox-border-container').animate({'top':'-='+(current_scroll-scroll[1])},100);
                         current_scroll = scroll[1];
                 }
         });

}
function get_measurments(pic_width,pic_height){
         var values = new Array();
         var page_size = ___getPageSize();
         var screen_width = page_size[2];
         var screen_height = page_size[3];
         values['max_height'] = page_size[2]+500;
         if(pic_width <= screen_width - 40 && pic_height <= screen_height - 100){
                 values['width'] = pic_width;
                 values['height'] = pic_height;
         }
         if(pic_width > screen_width - 40 || pic_height > screen_height - 100){
                 var side_ratio = pic_width/pic_height;
                 if(pic_width > screen_width - 60){
                         values['width'] = screen_width-60;
                         values['height'] = (screen_width-60)/side_ratio;
                 } else {
                         values['width'] = pic_width;
                         values['height'] = pic_height;
                 }
                 if(values['height'] > screen_height - 60){
                         values['height'] = screen_height-60;
                         values['width'] = (screen_height-60)*side_ratio;
                 }
         }
         var scroll = ___getPageScroll();
         current_scroll = scroll[1];
         values['left']= ((screen_width - values['width'])/2)-20;
         values['top'] = ((screen_height-values['height'])/2)-20+scroll[1];
         return values;
}
function resize(width,height,top,overlay_height,left){
         // for "bubble" effekt
         var bubble_w = 0;
         var bubble_h = 0;
         var bubble_left = 0;
         var bubble_top=0;
         if(width+20 > current_width){
                 bubble_left = -40;
                 bubble_w = bubble_w + 60 - bubble_left;
         } else if(width+20 < current_width){
                 bubble_left=40;
                 bubble_w=-40;
         } else {
         	 bubble_w=20;
         }
         if(height+20 > current_height){
         	 bubble_top = -20;
         	 bubble_h = bubble_h + 60 - bubble_top;
         } else if(height+20 < current_height) {
		bubble_top=20;
		bubble_h=-20;
         } else {
         	bubble_h=20;	 
         }
         //-------------------------
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
         if (jQuery.browser.msie && ie6) {
         	 $('#hw_awesomebox-bot-mid,#hw_awesomebox-top-mid').animate({'width':width,'height':10},800);
         	 $('#hw_awesomebox-right,#hw_awesomebox-left').animate({'height':height},800);
         }
         //-----------------------------------------         
         $('#hw_awesomebox-overlay').css('height', overlay_height);         
         $('#hw_awesomebox-border-container').animate({'top' : top+bubble_top,'left' : left+bubble_left,'height' : (height+bubble_h),'width' : (width+bubble_w)}, 800,function(){
         		 $('#hw_awesomebox-border-container').animate({'top': top,'height' : height+20,'left':left,'width' : width+20}, 400,function(){
                  	  current_width = width+20;
                  	  current_height = height+20;
                          $('#hw_awesomebox-img').css('width',width+20);
                          $('#hw_awesomebox-img').css('height',height+20);                          
                          show_pic();
                          if(pictures[current_img]['title'] != '' || pictures[current_img]['alt'] != ''){
                          	   // show infobox for each case
                          	   $('#hw_awesomebox-img-info').css('margin-top',0);
                                   $('#hw_awesomebox-img-info').show();
                                   if(pictures[current_img]['title'] != '' && pictures[current_img]['alt'] != ''){
                                   	   $('#hw_awesomebox-img-info').append('<span id="hw_awesomebox-info-title">'+pictures[current_img]['title']+'</span><br/><span id="hw_awesomebox-info-alt">'+ pictures[current_img]['alt']+'</span>');
                                   	   var hei = $('#hw_awesomebox-img-info').css('height');
                                   	   $('#hw_awesomebox-img-info').animate({'margin-top':'-'+hei},200,function(){});
                                   } else {                                   	   
                                   	   $('#hw_awesomebox-img-info').append('<span id="hw_awesomebox-info-title">'+pictures[current_img]['title']+'</span><span id="hw_awesomebox-info-alt">'+ pictures[current_img]['alt']+'</span>');
                                   	   var hei = $('#hw_awesomebox-img-info').css('height');
                                   	   $('#hw_awesomebox-img-info').animate({'margin-top':'-'+hei},200,function(){});
                                   }
                                   //-----------------
                                   //opacity
                                   //$('#hw_awesomebox-arrows').css('opacity',0.6);
                                   //$('#hw_awesomebox-close').css('opacity',0.6);                                   
				   //--------------------------                                   
                          }
                  });
         });
}


function ___getPageSize() {
        var xScroll, yScroll;
        if (window.innerHeight && window.scrollMaxY) {
                xScroll = window.innerWidth + window.scrollMaxX;
                yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
                xScroll = document.body.scrollWidth;
                yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
                xScroll = document.body.offsetWidth;
                yScroll = document.body.offsetHeight;
        }
        var windowWidth, windowHeight;
        if (self.innerHeight) {        // all except Explorer
                if(document.documentElement.clientWidth){
                        windowWidth = document.documentElement.clientWidth;
                } else {
                        windowWidth = self.innerWidth;
                }
                windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
                windowWidth = document.documentElement.clientWidth;
                windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
                windowWidth = document.body.clientWidth;
                windowHeight = document.body.clientHeight;
        }
        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
                pageHeight = windowHeight;
        } else {
                pageHeight = yScroll;
        }
        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){
                pageWidth = xScroll;
        } else {
                pageWidth = windowWidth;
        }
        arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
        return arrayPageSize;
};
/**
 / THIRD FUNCTION
 * getPageScroll() by quirksmode.com
 *
 * @return Array Return an array with x,y page scroll values.
 */
function ___getPageScroll() {
        var xScroll, yScroll;
        if (self.pageYOffset) {
                yScroll = self.pageYOffset;
                xScroll = self.pageXOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {         // Explorer 6 Strict
                yScroll = document.documentElement.scrollTop;
                xScroll = document.documentElement.scrollLeft;
        } else if (document.body) {// all other Explorers
                yScroll = document.body.scrollTop;
                xScroll = document.body.scrollLeft;
        }
        arrayPageScroll = new Array(xScroll,yScroll);
        return arrayPageScroll;
};
};
})(jQuery);
