/***************************************
    ASYNCHRONOUS IMAGE LOADING CODE
****************************************/

// For all object with class "asyncImaLoad" (loader), function read
// image path from title attribute, create new Image object,
// next assign image from path to Image object and 
// append it to loader 
function setupLoadingAsynchronousImages()
{
    // for each object with class "asyncImgLoad"
    $('.asyncImgLoad').each(
        function()
        {   
            // save handle to loader - caintainer which we gona insert loaded image    
            var loader = $(this);
            // get image path from loader title attribute
            var imagePath = loader.attr('title');
            // create new image object
            var img = new Image();
            // set opacity for image to maximum
            // value 0.0 means completly transparent
            $(img).css("opacity", "0.0")
            // next we set function wich gona be caled then
            // image load is finished  
                .load(
                    function() 
                    {
                        // insert loaded image to loader object 
                        // and remove unnecessary title attribute
                        loader.append(this).removeAttr('title');
                        // for inserted image we set margin to zero
                        // opacity to max and fire up 500ms opacity animation 
                        $(this)
                            .css("margin", "0px")
                            .css("opacity", "0.0")
                            .animate({opacity: 1.0}, 500,
                                function()
                                {
                                    // after animation we remove loader background image 
                                    loader.css("background-image", "none");
                                }
                            );
                    }
                // set new value for attribute src - this means: load image from imagePath    
                ).attr('src', imagePath);                        
        }
    );
} // end of function setupLoadingAsynchronousImages
 

/**************************
   SMART IMAGE PEVIEW
**************************/

// global variables that keep width and height previewed image
var g_imagePreviewWidth = 0;
var g_imagePreviewHeight = 0;
var g_imgExtraYOffset = 0;
// variable help prevent hover overlap on real web serwer
// then user move cursor on other thumb, function compare new thumb handle 
// with one saved in this variable, then there is difference this is signal for
// old hovered action that new action is actualy in work
var g_imagePreviewHandle = null;
// cursor X and Y postion in hover moment
var g_onHoverX = 0;
var g_onHoverY = 0;
var g_preTopChanged = false;
// function display image preview, the path to image is saved in attribute "rel"
function setupToolTipImagePreview()
{
    // for every object with class "imgTipLeftTop",
    // "imgTipRightTop" and "imgTipCenterTop"
    $(".imgTipLeftTop, .imgTipRightTop, .imgTipCenterTop").hover(function(e)
    {
        // save handle in global variable
        g_imagePreviewHandle = this;
        // image preview container x,y offset from the cursor position
        var offsetX = 0;       
        var offsetY = -15;
        // keep hovered div handle in local variable
        var hoveredObject = this;        
        // get path to image file
        var imagePath = $(this).attr("rel");
        // find image thumb in div and decrease opaque to 50% in 300ms animation
        // before animation can start, we must preventive stop earlier one
        $(this).find("img").stop().animate({opacity:0.5},300); 
  
        // adding to page image preview container
        $("body").append(
            "<div id='imgPreview'><div id='imgPreviewImg'></div><div id='imgPreviewDesc'></div></div>");
            
        // hide image preview container, this is necessary to correctly call show function
        $("#imgPreview").hide(); 
           
            // create new image object
            var img = new Image();
            // bind function to object which gona be called when new image loading is finished
            $(img).load(function() 
            {
                // check image preview overlap
                if(g_imagePreviewHandle != hoveredObject)
                {
                    // in new image preview is in progress function return control
                    return;
                }
                // save image width and height in global variables
                g_imagePreviewWidth = img.width;
                g_imagePreviewHeight = img.height;
                g_imgExtraYOffset = 0;
                
                if($(hoveredObject).hasClass("imgTipLeftTop"))
                {
                    offsetX = -30 -g_imagePreviewWidth;
                } else
                if($(hoveredObject).hasClass("imgTipRightTop"))
                {
                    offsetX = 30;
                } else
                if($(hoveredObject).hasClass("imgTipCenterTop"))
                {
                    offsetX = -(g_imagePreviewWidth / 2);
                }

                // get image description ant put text in image desc container
                var imgDesc = $(hoveredObject).find(".desc");
                if(imgDesc.length != 0)
                {
                    var txt = $(imgDesc).html();
                    var search = "<br";
                    if(g_browserDetect.browser == "Explorer" || g_browserDetect.browser == "Opera")
                    {
                        search = "<BR";
                    }
                    var count = 1;
                    var start = 0;
                    var result = 0;
                    while((result = txt.indexOf(search, start)) != -1)
                    {
                       start = result + 3;
                       count++; 
                    }
                    g_imgExtraYOffset = 14 * count; 
                    $("#imgPreview").find("#imgPreviewDesc").html(txt);
                    
                } else
                {
                    $("#imgPreview").find("#imgPreviewDesc").remove();
                }
                
                // check space available space in browser window
                g_onHoverX = e.pageX;
                g_onHoverY = e.pageY;
                var browserWidth = $(window).width();
                var browserHeight = $(window).height();
                var preLeft = e.pageX + offsetX;
                var preTop = e.pageY + offsetY - g_imagePreviewHeight - g_imgExtraYOffset;
                
                if(g_imagePreviewHeight > e.clientY)
                {                   
                   preTop += g_imgExtraYOffset + g_imagePreviewHeight - (offsetY*2);
                   g_preTopChanged = true;
                }
                if(preLeft < 0)
                {
                    preLeft = 30 + (e.pageX - g_onHoverX);
                }
                // check space on right
                if(preLeft + g_imagePreviewWidth > browserWidth)
                {
                    preLeft = browserWidth - g_imagePreviewWidth - 30 + (e.pageX - g_onHoverX) + (e.pageX - e.clientX);
                }                                
                
                // add image to preview container
                $("#imgPreviewImg").html(this);
                // set height of container
                $("#imgPreviewImg").css("height", g_imagePreviewHeight);        

                // set image preview container style,
                // we must set width of container, the height is calculated automaticly based on image height
                // we must set also image container position, make it visible and set opacity to max
                $("#imgPreview").css("width", (g_imagePreviewWidth)+"px")
                    .css("left", preLeft + "px")
                    .css("top", preTop + "px")
                    .css("visibility", "visible")
                    .css("opacity", "1.0")
                    .show("fast");
       
                // image opacity we animate separately
                $(this).css("margin", "0px").css("padding", "0px").css("opacity", "0.0").animate({opacity: 1.0}, 800);
                // we must also restore orginal opacity of hovered thumb
                $(hoveredObject).find("img").stop().animate({opacity:1.0},300);
                 
            // set new value for "src" attribute, this me     
            }).attr("src", imagePath);

    },
    // when hover is out, we need to remove #imagePreview container from page
    function()
    {
        // remove image preview container from page
        $("#imgPreviewDesc").remove();
        $("#imgPreviewImg").find("img").stop().remove();
        $("#imgPreviewImg").remove();
        $("#imgPreview").stop().remove();
        // we must also restore orginal opacity of hovered thumb  
        $(this).find("img").stop().animate({opacity:1.0},300);
        g_imagePreviewHandle = null;
        g_preTopChanged = false; 
        
    });    
    
    // for every image preview thumb we bind function to mouse move event, 
    // this allow user to move the tooltip with mouse cursor in are of the thumb
    $(".imgTipLeftTop, .imgTipRightTop, .imgTipCenterTop").mousemove(
        function(e)
        {
            // image preview container x,y offset form the cursor position
            var offsetX = 0;
            if($(this).hasClass("imgTipLeftTop"))
            {
              offsetX = -30 - g_imagePreviewWidth;
            } else
            if($(this).hasClass("imgTipRightTop"))
            {
              offsetX = 30;
            } else
            if($(this).hasClass("imgTipCenterTop"))
            {
              offsetX = -g_imagePreviewWidth / 2;
            }
            
            var offsetY = -15;        
            
            // check space available space in browser window 
            var preLeft = e.pageX + offsetX;
            var preTop = e.pageY + offsetY - g_imagePreviewHeight - g_imgExtraYOffset;
            // check space on top
            
            if(g_imagePreviewHeight > e.clientY || g_preTopChanged == true)
            {
                if(g_preTopChanged == true)
                {
                   preTop += g_imgExtraYOffset + g_imagePreviewHeight - (offsetY*2);
                }
            }
            // check space on left
            if(preLeft < 0)
            {
              preLeft = 30 + (e.pageX - g_onHoverX);
            }
            // check space on right
            var browserWidth = $(window).width(); 
            if(preLeft + g_imagePreviewWidth > browserWidth)
            {
              preLeft = browserWidth - g_imagePreviewWidth - 30 + (e.pageX - g_onHoverX) + (e.pageX - e.clientX);
            }            
            
            // change image preview container position
            $("#imgPreview")
                .css("top",preTop + "px")
                .css("left", preLeft + "px");
        });
       
} // end of function setupToolTipImagePreview

/***************************************
    CODE FOR TABS
****************************************/
// handle to selected tab
var g_selectedTab = null;
var g_tabsBtnColor = "#BCBBBA";

function setupTabs()
{
    // selecting all tab switch on
    var tabs = $(".tabsBtn");

    // for every tab we doing the same things
    for(var i = 0; i < tabs.length; i++)
    {
        // veriable for object with class tabsDefaultTab
        var defaultTab = null;
        // lets find default tab element
        defaultTab = $(tabs[i]).find(".tabsDefaultTab");
        // if currently analysed tab contains the default tab element 
        if(0 != defaultTab.length)
        {
            var tabSource = $(tabs[i]).find(".tabsSource").text();
            $(tabSource).css("visibility", "visible").css("top", 0);
            $(tabs[i]).each(function(){g_selectedTab = this;});
            //$(tabs[i]).css("background-color", g_tabsBtnColor);
			//$(tabs[i]).css("background-image", "url('/images/tabbtnwhite.jpg')");
			//$(tabs[i]).css("background-repeat", "repeat-x");
			$(tabs[i]).css("color", "#C42126");
            break;
        }
    } // for


    $(".tabsBtn").click(
        function()
        {
             if(g_selectedTab == this)
             {
                return;
             } 
             if(g_selectedTab != null)
             {
                //$(g_selectedTab).css("background-color", "#282828");
				//$(g_selectedTab).css("background-image", "url('/images/tabbtnblack.jpg')");
				//$(g_selectedTab).css("background-repeat", "repeat-x");
				$(g_selectedTab).css("color", "white");
             }
             var oldSource = $(g_selectedTab).find(".tabsSource").text();
             g_selectedTab = this;
             //$(this).css("background-color", g_tabsBtnColor);
			//$(this).css("background-image", "url('/images/tabbtnwhite.jpg')");
			//$(this).css("background-repeat", "repeat-x");
			$(this).css("color", "#C42126");
             
             $(oldSource).animate({opacity: 0.0}, 200, 
                function()
                {
                    $(this).css("visibility", "hidden");
                    var tabSource = $(g_selectedTab).find(".tabsSource").text();
                    $(tabSource)
                        .css("opacity", 0.0)
                        .css("top", 0)
                        .css("visibility", "visible")
                        .animate({opacity: 1.0}, 400); 
                }
             );
        }
    );

    $(".tabsBtn").hover(
        function()
        {
            if(this != g_selectedTab)
            {
                //$(this).css("background-color", g_tabsBtnColor);
				//$(this).css("background-image", "url('/images/tabbtnwhite.jpg')");
				//$(this).css("background-repeat", "repeat-x");
				$(this).css("color", "#C42126");
            
            }
        },
        function()
        {
            if(this != g_selectedTab)
            {
                //$(this).css("background-color", "#282828");
				//$(this).css("background-image", "url('/images/tabbtnblack.jpg')");
				//$(this).css("background-repeat", "repeat-x");
				$(this).css("color", "white");
            
            }
        }
    );
} // end of function setupTabs  
       
this.imagePreview = function(){    
    /* CONFIG */
        
        xOffset = 10;
        yOffset = 30;
        
        // these 2 variable determine popup's distance from the cursor
        // you might want to adjust to get the right result
        
    /* END CONFIG */
    $("a.preview").hover(function(e){
        this.t = this.title;
        this.title = "";    
        var c = (this.t != "") ? "<br/>" + this.t : "";
        $("body").append("<p id='preview'><img width='400px' src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");                                 
        $("#preview")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px")
            .show("fast"); 
		//alert($("#preview").html() + this.href);
    },
    function(){
        this.title = this.t;    
        $("#preview").remove();
    });    
    $("a.preview").mousemove(function(e){
        $("#preview")
            .css("top",(e.pageY - xOffset) + "px")
            .css("left",(e.pageX + yOffset) + "px");
    });            
};


// starting the script on page load
$(document).ready(function(){
    imagePreview();
});
       
/***************************************
    MAIN CODE - CALL THEN PAGE LOADED
****************************************/
       
// binding action to event onload page
$(document).ready(
    function()
    {

        // common.js
        //setupGlobal();
        //setupCommunityButtons();            
        //setupToolTipText();
        //setupSearchBox();
        //setupCufonFontReplacement();
        //setupSideBarMiniSlider();
        //setupLinkLightBox();
        //setupSidebarTabsPanel();
        setupLoadingAsynchronousImages();
        setupToolTipImagePreview();
        //setupTextLabelImagePreview();
        //setupFaderMoverSlider();   
        // this file
        //setupAdditionalCufonFontReplacement();
        //setupNewsBar();
        setupTabs();
        /*if($("#accordionContainer").length > 0)
        {
            // call this functions only if accordion is included
            setupLoadingAsynchronousImagesForAccordion();
            setupLoadingAsyncSlideStripImages();            
            setupAccordionImageSlider();
            setupAccordionControlPanel();        
            setupAccordionAutoPlay();
        }
        setupLatestNews();
		*/
    }
);




