// JavaScript Document


var height_ = jQuery.fn.height;
jQuery.fn.height = function() {
    if (this[0] == window && jQuery.browser.opera && jQuery.browser.version >= 9.50) 
        return window.innerHeight;
    else 
        return height_.apply(this, arguments);
};


function addLbox(adr, i, l, Lbl) {
    //var adr = [];
    //adr = s.split(",")
    //var Lbl = [];
    //Lbl = lbLabel.split("||");
    
    
    var writeLbox = '<div class="lBoxWrap"></div>';
    writeLbox += '<div id="outerLbox"><div id="middleLbox"><div id="innerLbox"><div class="closewrapLbox"><a href="#" class="closeLbox">close</a></div><div class="imgwrapLbox"><div class="imgwrapInnerLbox"></div></div><div class="pgwrapLbox"><a href="#" class="nextLbox">next</a><a href="#" class="previousLbox OverImage">previous</a><a href="#" class="nextLbox OverImage">next</a><a href="#" class="previousLbox">previous</a><div class="labelLbox"></div></div></div> </div></div>';
    
    $('body').prepend(writeLbox);
    
    $('.lBoxWrap').height($('body').height() + 30 + 'px');
    $('.lBoxWrap').width($('body').width() + 'px');
    $('.lBoxWrap').click(function(event) {
        event.stopPropagation();
    });
	
	$('.lBoxWrap').css({
        "visibility": "visible",
        "opacity": "0"
    });
    $('.lBoxWrap').animate({
        opacity: "0.8"
    }, {
        "duration": 800
    });
    var wH = $(window).height() + 'px';
    $('#outerLbox').height(wH);
    $(window).resize(function() {
        addLboxResBody();
    });
    
    $('.closeLbox').click(function() {
        $(window).unbind('resize', addLboxResBody);
        $('.imgwrapLbox').stop();
        $('.imgwrapLbox img').stop().animate({
            opacity: "0"
        }, 800, function() {
            $('.imgwrapLbox img').remove();
            $('#outerLbox').animate({
                opacity: "0"
            }, 600, function() {
                $("#outerLbox").remove();
                $('.lBoxWrap').animate({
                    opacity: "0"
                }, 1000, function() {
                    $(".lBoxWrap").remove()
                });
            });
        });
        
        
        channelv.Page.updateDblClickAd('/CloseLightBox');
        channelv.Page.trackPage('/CloseLightBox', 'Close Lightbox');
        
        return false
    });
    
    $('.imgwrapLbox').click(function() {
        $(window).unbind('resize', addLboxResBody);
        $('.imgwrapLbox').stop();
        $('.imgwrapLbox img').stop().animate({
            opacity: "0"
        }, 800, function() {
            $('.imgwrapLbox img').remove();
            $('#outerLbox').animate({
                opacity: "0"
            }, 600, function() {
                $("#outerLbox").remove();
                $('.lBoxWrap').animate({
                    opacity: "0"
                }, 1000, function() {
                    $(".lBoxWrap").remove()
                });
            });
        });
		channelv.Page.updateDblClickAd('/CloseLightBox');
		channelv.Page.trackPage('/CloseLightBox', 'Close Lightbox');
        return false
    });
    
    $('#innerLbox').click(function(event) {
        event.stopPropagation();
    });
    $('.OverImage').click(function(event) {
        event.stopPropagation();
    });
    
    var myImg = new Image();
    var pathImg = adr[i];
    var titleImg = "Close img" + i;
    $(myImg).load(function() {
        var imgH = myImg.height;
        var imgW = myImg.width;
        var w_H = $(window).height();
        var w_W = $(window).width();
        $('#innerLbox .imgwrapLbox .imgwrapInnerLbox').append(this);
        $('.labelLbox').html(Lbl[i]);
        $('#innerLbox ').animate({
            width: imgW + 20
        }, 1000);
        $('#innerLbox .imgwrapLbox').animate({
            width: imgW,
            height: imgH
        }, 1000, function() {
        
        
            if (i == 0) $('.previousLbox').css({
                display: "none"
            });
            if (i == l - 1) $('.nextLbox').css({
                display: "none"
            });
            
            
            $('.imgwrapLbox img').css({
                opacity: 0
            }).css({
                visibility: "visible"
            }).animate({
                opacity: 1
            }, 800);
            $('.pgwrapLbox').css({
                visibility: "visible"
            });
            $('.closeLbox').css({
                visibility: "visible"
            });
            var revH = -imgH - 10;
            $('.OverImage').css({
                height: imgH,
                marginTop: revH
            });
            
            if ($.browser.version < 7) $.scrollTo('#header', 800);
        });
    });
    
    $('.nextLbox').click(function() {
        if (i < l) {
            i++;
            $('.pgwrapLbox').css({
                visibility: "hidden"
            });
            $('.nextLbox').css({
                display: "none"
            });
            $('.previousLbox').css({
                display: "none"
            });
            $('.imgwrapLbox .imgwrapInnerLbox').animate({
                opacity: "0"
            }, 600, function() {
                $('.imgwrapLbox .imgwrapInnerLbox img').remove();
                pathImg = adr[i];
                titleImg = "Close img" + i;
                var myImg = new Image();
                $(myImg).load(function() {
                    $(myImg).ready(function() {
                        $('#innerLbox').stop();
                        $('#innerLbox .imgwrapLbox').stop();
                        var imgH = myImg.height;
                        var imgW = myImg.width;
                        $('#innerLbox .imgwrapLbox .imgwrapInnerLbox').append(myImg);
                        $('.labelLbox').html(Lbl[i]);
                        $('#innerLbox .imgwrapLbox').animate({
                            width: imgW,
                            height: imgH
                        }, 600);
                        $('#innerLbox ').animate({
                            width: imgW + 20
                        }, 600, function() {
                            $('.imgwrapLbox .imgwrapInnerLbox img').css({
                                opacity: 1
                            })
                            $('.imgwrapLbox .imgwrapInnerLbox').css({
                                opacity: 0
                            }).css({
                                visibility: "visible"
                            }).animate({
                                opacity: 1
                            }, 600, function() {
                                if (i == l - 1) 
                                    $('.nextLbox').css({
                                        display: "none"
                                    });
                                else 
                                    $('.nextLbox').css({
                                        display: "block"
                                    });
                                $('.previousLbox').css({
                                    display: "block"
                                });
                                $('.pgwrapLbox').css({
                                    visibility: "visible"
                                });
                                var revH = -imgH - 10;
                                $('.OverImage').css({
                                    height: imgH,
                                    marginTop: revH
                                });
                            });
                        });
                    });
                });
                $(myImg).attr({
                    src: pathImg,
                    title: titleImg
                });
            });
        };
        channelv.Page.updateDblClickAd(pathImg);
        channelv.Page.trackPage(pathImg, 'LightBoxImage+'+i);
        return false
    });
    $('.previousLbox').click(function() {
        if (i > -1) {
            i--;
            $('.pgwrapLbox').css({
                visibility: "hidden"
            });
            $('.nextLbox').css({
                display: "none"
            });
            $('.previousLbox').css({
                display: "none"
            });
            
            $('.imgwrapLbox .imgwrapInnerLbox').animate({
                opacity: "0"
            }, 600, function() {
                $('.imgwrapLbox .imgwrapInnerLbox img').remove();
                var myImg = new Image();
                pathImg = adr[i];
                titleImg = "Close img" + i;
                $(myImg).load(function() {
                    $(myImg).ready(function() {
                        $('#innerLbox').stop();
                        $('#innerLbox .imgwrapLbox').stop();
                        var imgH = myImg.height;
                        var imgW = myImg.width;
                        $('#innerLbox .imgwrapLbox .imgwrapInnerLbox').append(myImg);
                        $('.labelLbox').html(Lbl[i]);
                        $('#innerLbox .imgwrapLbox').animate({
                            width: imgW,
                            height: imgH
                        }, 600);
                        $('#innerLbox ').animate({
                            width: imgW + 20
                        }, 600, function() {
                            $('.imgwrapLbox .imgwrapInnerLbox img').css({
                                opacity: 1
                            })
                            $('.imgwrapLbox .imgwrapInnerLbox ').css({
                                opacity: 0
                            }).css({
                                visibility: "visible"
                            }).animate({
                                opacity: 1
                            }, 600, function() {
                                if (i == 0) 
                                    $('.previousLbox').css({
                                        display: "none"
                                    });
                                else 
                                    $('.previousLbox').css({
                                        display: "block"
                                    });
                                $('.nextLbox').css({
                                    display: "block"
                                });
                                $('.pgwrapLbox').css({
                                    visibility: "visible"
                                });
                                var revH = -imgH - 10;
                                $('.OverImage').css({
                                    height: imgH,
                                    marginTop: revH
                                });
                            });
                        });
                    });
                });
                $(myImg).attr({
                    src: pathImg,
                    title: titleImg
                });
            });
        };
        return false
    });
    
    
    $(myImg).css({
        visibility: "hidden"
    });
    $(myImg).attr({
        src: pathImg,
        title: titleImg
    });
    
    channelv.Page.updateDblClickAd(pathImg);
    channelv.Page.trackPage(pathImg, 'LightBoxImage+'+i);
    
}



function addLboxResBody() {
    $('.lBoxWrap').height($('body').height() + 30 + 'px');
    $('.lBoxWrap').width($('body').width() + 'px');
    var wH = $(window).height();
    var bH = $('body').height();
    var bW = $('body').width();
    var tM = (bH / 2 - wH);
    $('#outerLbox').height(wH + 'px');
    $('#outerLbox').width(bW + 'px');
}
