﻿(function($) {
    jQuery.fn.popUp = function($options){
        // set document height
        $.fn.popUp.DocHeight = $(document).height();
	    // extend the options
	    $.fn.popUp.opts = $.extend({},$.fn.popUp.Default,$options);
        // check type
        switch($.fn.popUp.opts.type){
            case 'regular':
                $.fn.popUp.show();
                break;
            case 'regularFunc':
                $.fn.popUp.show();
                break;
            case 'image':
                $.fn.popUp.thumb();
                break;
            default:
                $.fn.popUp.show();
                break;
        }
    };
    jQuery.fn.popUp.opts = {
		    type     : 'regular',
		    func     : false,
		    imageArr : null,
		    content  : '',
		    btnClose  : true,
		    scrollTo : 0
	    };
    // set the default options
	jQuery.fn.popUp.Default = {
		    type     : 'regular',
		    func     : false,
		    imageArr : null,
		    content  : 'Det blev något fel försök senare',
		    btnClose : true, 
		    scrollTo : 0
	    };
    jQuery.fn.popUp.show = function(e){
        var $this = '';
        $.fn.popUp.DocWidth = $(document).width();
        if($("div[@id$='ImgPop']").attr('id') == 'ImgPop'){
            $this = $("div[@id$='ImgPop']");
        }else{
            $this = $(document.createElement('div'))
                .attr('id', 'ImgPop')
                .appendTo(document.forms[0]);
        }
        
        // The Fade Background
        var $thisBg = $(document.createElement('div'))
            .height($(document).height())
            .width($(document).width())
            .addClass('popBg')
            .appendTo($this);
        
        // PopUp Content
        var $thisContent = $(document.createElement('div'))
            .addClass('popCont')
            .appendTo($this);
            
        if($.fn.popUp.opts.func){
            $thisContent.append($.fn.popUp.opts.content);
        }else{
            $thisContent.text($.fn.popUp.opts.content);
        }
        if($.fn.popUp.opts.type == 'image'){
            if($.browser.mozilla){
                $thisContent.css({
                    left: ($(document).width()-$thisContent.width())/2,
                    top: ($(document).height()-$thisContent.height())/2
                });
                if($thisContent.height()+15 > $(document).height()){
                    $thisBg.height($thisContent.height()+15);
                }
                if($thisContent.find('.LargeImg').find('img').width() > $.fn.popUp.DocWidth){
                    $thisBg.width($thisContent.find('.LargeImg').find('img').width()+40);
                }
                $.scrollTo({
                    top: ($(document).height()-$thisContent.height())/2-5, 
                    left: ($(document).width()-$thisContent.width())/2
                    }, 500);
            }
            if($.browser.msie){
                $.fn.popUp.IeFirstLargeImgFix();
            }
        }else{
            $thisContent.css({
                left: ($(document).width()-$thisContent.width())/2,
                top: ($(document).height()-$thisContent.height())/2
            });
        }
        if($.fn.popUp.opts.btnClose){
            // Close PopUp Content
            $(document.createElement('div'))
                .text('x')
                .addClass('closeBtn')
                .appendTo($thisContent)
                .click(function(){
                    $thisContent.empty();
                    $thisContent.remove();
                    $this.empty();
                    $this.remove();
                    $this = null;
                    $thisContent = null;
                    if($.fn.popUp.opts.scrollTo != 0){
                        $.scrollTo($("div[@id$='"+$.fn.popUp.SrcollId+"']"), 500);
                    }
                })
                .hover(
                    function(){
                        $(this).toggleClass('closeBtnHover');
                    },
                    function(){
                        $(this).toggleClass('closeBtnHover');
                    }
                );
        }
    };
    jQuery.fn.popUp.thumb = function(){
        
        var $imgArr = jQuery.makeArray($.fn.popUp.opts.imageArr);
        if($imgArr.length > 1){
            var $imgMl = 0;
            var $imgNum = 1;
            var $imgLength = $imgArr.length*92;
            var $imgRow = $imgArr.length / 6;
            var $imgMaxL = (parseInt($imgRow.toString().charAt(0))*6)*92;
            $imgRow = $imgRow.toString().charAt(0);
            $.fn.popUp.SelectedImgPath = $.fn.popUp.ImagePath+$.fn.popUp.SelectedImgPath;
                 
            //Image Contianer PlaceHolder
            var $ImgContian = $(document.createElement('div'))
                .addClass('imgContian');
                
            //Image Item PlaceHolder Slide Left
            var $ArrL = $(document.createElement('div'))
                .attr('id','pnlArrL')
                .appendTo($ImgContian);
                
            $(document.createElement('img'))
                .addClass('imgArrL')
                .attr('src', 'image/imgArrowL.gif')
                .attr('alt', '')
                .appendTo($ArrL)
                .click(function(){
                    if($imgNum > 1){
                        if($imgNum-1 == $imgRow){$imgMl = $imgLength-$imgMaxL; $('.imgArrR').fadeIn(300);}
                        else{$imgMl = 6*92;}
                        $($ImgItem).animate({ 'marginLeft': '+='+$imgMl }, 200, 'easeOutBack');
                    }
                    if($imgNum != 1){$imgNum--;}
                    if($imgNum == 1){$(this).fadeOut(300);}
                })
                .hover(
		            function () {
			            $(this).attr('src', $(this).attr('src').replace('.gif','')+'H.gif');
		            }, 
		            function () {
			            $(this).attr('src', $(this).attr('src').replace('H',''));
		            }   
	            );
            
            //Image Content PlaceHolder
            var $ImgCont = $(document.createElement('div'))
                .addClass('imgCont')
                .appendTo($ImgContian);
                
            //Image Items PlaceHolder
            var $ImgItem = $(document.createElement('div'))
                .addClass('imgItem')
                .css('width', $imgLength)
                .appendTo($ImgCont);
            
            //Insert Image To Image Items
            var _imgPath = null;
            for(var i=0; i <= $imgArr.length; i++){
                if($.browser.msie){
                    _imgPath = $.fn.popUp.ImagePath+$imgArr[i];
                    _imgPath = _imgPath.replace(document.URL.replace('Default1.aspx', ''), '');
                }else{
                    _imgPath = $.fn.popUp.ImagePath+$imgArr[i];
                }
                $(document.createElement('img'))
                    .attr('src', _imgPath.replace('//', '/'))
                    .attr('alt', '')
                    .css('opacity', 0.5)
                    .hover(
	                    function () {
		                    $(this).fadeTo('fast', 1);
	                    }, 
	                    function () {
	                        if($(this).attr('src') != $.fn.popUp.SelectedImgPath){$(this).fadeTo('normal', 0.5);}
	                    }
                    )
                    .click(function(){
                        if($.fn.popUp.SelectedImgPath != $(this).attr('src')){
                            $(this).parent('div').find("img[src$="+$.fn.popUp.SelectedImgPath+"]").fadeTo('fast', 0.5);
                            $.fn.popUp.SelectedImgPath = $(this).attr('src');
                            $.fn.popUp.SelectedImg();
                        }
                    })
                    .appendTo($ImgItem);
            }
            $($ImgItem).find("img[src$="+$.fn.popUp.SelectedImgPath+"]").css('opacity', 1.0);
            
            $(document.createElement('div'))
                .css('clear', 'both')
                .appendTo($ImgCont);
                           
            var $ArrR = $(document.createElement('div'))
                .attr('id','pnlArrR')
                .appendTo($ImgContian);
                
            $(document.createElement('img'))
                .addClass('imgArrR')
                .attr('src', 'image/imgArrowR.gif')
                .attr('alt', '')
                .appendTo($ArrR)
                .click(function(){
                    if($imgNum <= $imgRow){
	                    if($imgNum == $imgRow){$(this).fadeOut(300);$imgMl = $imgLength-$imgMaxL;}
	                    else{$imgMl = 6*92;}
	                        $($ImgItem).animate({ 'marginLeft': '-='+$imgMl}, 200, 'easeOutBack');
	                        $imgNum++;
	                    }
                    if($imgNum != 1){$('.imgArrL').fadeIn(300);}
                })
                .hover(
		            function () {
			            $(this).attr('src', $(this).attr('src').replace('.gif','')+'H.gif');
		            }, 
		            function () {
			            $(this).attr('src', $(this).attr('src').replace('H',''));
		            }   
	            );
            
            $(document.createElement('div'))
                .css('clear', 'both')
                .appendTo($ImgContian);
            
            var $largeImgContianer = $(document.createElement('div')).addClass('LargeImg');
            var $largeImgCenter = $(document.createElement('center')).appendTo($largeImgContianer);
            
            var $LargeImg = new Image();
            if($.browser.msie){
                var $LargeImgPath = $.fn.popUp.SelectedImgPath.replace(document.URL.replace('Default1.aspx', ''), '');
                $LargeImgPath = $LargeImgPath.replace('thumb/','');
                $LargeImgPath = $LargeImgPath.replace('//','/');
            }else{
                var $LargeImgPath = $.fn.popUp.SelectedImgPath.replace('thumb/','');
            }
            
            $($LargeImg).attr('src', $LargeImgPath).attr('alt', '').attr('id','imgLarge_00');
            
            if($.browser.mozilla){
                $($LargeImg).load(function(){
                    $(this).appendTo($largeImgCenter);
                    
                    if($(this).height()+118 > $.fn.popUp.DocHeight){
                        $.fn.popUp.largeImgIe.bgHeight = $(this).height()+138;
                    }
                    var $tempCont = $(document.createElement('div'))
                        .append($largeImgContianer)
                        .append($ImgContian);
                    $.fn.popUp.opts.content = $tempCont;
                    $.fn.popUp.show();
                    $tempCont = null;
                });
            }else{
                $($LargeImg).appendTo($largeImgCenter);
                var $tempCont = $(document.createElement('div'))
                    .append($largeImgContianer)
                    .append($ImgContian);
                $.fn.popUp.opts.content = $tempCont;
                $.fn.popUp.show();
                $tempCont = null;
            }
        
        }else{
            var $largeImgContianer = $(document.createElement('div')).addClass('LargeImg');
            var $largeImgCenter = $(document.createElement('center')).appendTo($largeImgContianer);
            
            var $LargeImg = new Image();
            var $LargeImgPath = $.fn.popUp.ImagePath+$.fn.popUp.SelectedImgPath.replace('thumb/','');
            $($LargeImg).attr('src', $LargeImgPath).attr('alt', '').attr('id','imgLarge_00');
            
            if($.browser.mozilla){
                $($LargeImg).load(function(){
                    $(this).appendTo($largeImgCenter);
                    
                    if($(this).height()+118 > $.fn.popUp.DocHeight){
                        $.fn.popUp.largeImgIe.bgHeight = $(this).height()+138;
                    }
                    var $tempCont = $(document.createElement('div'))
                        .append($largeImgContianer);
                    $.fn.popUp.opts.content = $tempCont;
                    $.fn.popUp.show();
                    $tempCont = null;
                });
            }else{
                $($LargeImg).appendTo($largeImgCenter);
                var $tempCont = $(document.createElement('div'))
                    .append($largeImgContianer);
                $.fn.popUp.opts.content = $tempCont;
                $.fn.popUp.show();
                $tempCont = null;
            }
        }
    }
    jQuery.fn.popUp.SelectedImg = function(){
        if($.browser.mozilla){
            $.fn.popUp.loadMoz();
        }else{
            $.fn.popUp.loadIe();
        }
    }
    jQuery.fn.popUp.loadMoz = function(){
        var $loader = new Image();
        $($loader).attr('src',$.fn.popUp.SelectedImgPath.replace('thumb/',''));
        $($loader).load(function(){
            $("div[class$=LargeImg]").find('center').fadeTo(300, 0.0, function(){
                $("div[class$=LargeImg]").find('center').find('img').remove();
                $("div[class$=LargeImg]").find('center').append($loader);
                
                var $tempImgH = $(this).height()+118;
                var $tempMoveL = null;
                var $tempMoveT = null;
                var $tempBgH = false;
                var $tempBgW = false;
                
                if($("div[class$=imgContian]").width() > $($loader).width()){
                    $tempMoveL = ($(document).width()-$("div[class$=popCont]").width())/2;    
                }else{
                    if($($loader).width() >= $.fn.popUp.DocWidth){
                        $tempMoveL = 10;
                        $tempBgW = false;
                        $("div[class$=popBg]").width($($loader).width()+40);
                    }
                    else{
                        if($("div[class$=popBg]").width() > $.fn.popUp.DocWidth){
                            $("div[class$=popBg]").width($.fn.popUp.DocWidth);
                        }
                        $tempMoveL = ($.fn.popUp.DocWidth-$($loader).width())/2; 
                    }
                }
                if($.fn.popUp.DocHeight < $tempImgH){
                    $tempMoveT = 10;
                    $("div[class$=popBg]").height($($loader).height()+138);
                    $tempBgH = false;
                }else{
                    $tempMoveT = ($.fn.popUp.DocHeight-$($loader).height()-118)/2;
                    if($("div[class$=popBg]").height() > $.fn.popUp.DocHeight){
                       $("div[class$=popBg]").height($.fn.popUp.DocHeight);
                    }
                }
                $("div[class$=popCont]").animate({
                    left: $tempMoveL,
                    top: $tempMoveT
                },500, 'easeInOutQuint', function(){
                    $.scrollTo({
                        top: $tempMoveT-5,
                        left: $tempMoveL
                    }, 500);
                    $("div[class$=LargeImg]").find('center').fadeTo(300, 1.0);
                });
            });
        });
    }
    jQuery.fn.popUp.loadIe = function(){
        var $loaderIe = new Image();
        $($loaderIe).attr('src', $.fn.popUp.SelectedImgPath.replace('thumb/','')).attr('alt', '').attr('id','imgLarge_00');
    
        var _imgTemp=$("div[class$=LargeImg]").find('center');
        $("div[class$=LargeImg]").find('center').fadeTo(300, 0.0, function(){
            $(_imgTemp).find('img').remove();
            $(_imgTemp).append($loaderIe);
            $.fn.popUp.loadIeFix();
        });
    }
    jQuery.fn.popUp.loadIeFix = function(){
        window.setTimeout(function() {
            var _getStateLoad = document.getElementById($("div[@class$='popCont']").find('.LargeImg').find('center').find('img').attr('id').toString());
            if(_getStateLoad.complete == true){
                var tempMoveEiL = null;
                var tempMoveEiT = null;
                var tempMoveEiW = null;
                var tempEiBgH = false;
                var tempEiBgW = false;
                
                if(_getStateLoad.width < 630){
                    tempMoveEiW = 630;
                    tempEiBgW = true;
                }else{
                    if(_getStateLoad.width > $.fn.popUp.DocWidth){
                        tempEiBgW = false;
                        $("div[class$=popBg]").width(_getStateLoad.width+40);
                    }else{
                        tempEiBgW = true;
                    }
                    tempMoveEiW = _getStateLoad.width;
                }
                if(_getStateLoad.height+125 > $.fn.popUp.DocHeight){
                    tempMoveEiT = 10;
                    $("div[class$=popBg]").height(_getStateLoad.height+138);
                    tempEiBgH = false;
                }else{
                    tempMoveEiT = ($.fn.popUp.DocHeight-_getStateLoad.height-118)/2;
                    tempEiBgH = true;
                }
                if(!tempEiBgW){
                    tempMoveEiL = 10;
                }else{
                    tempMoveEiL = ($.fn.popUp.DocWidth-tempMoveEiW)/2;
                }
                
                $("div[class$=popCont]").animate({
                    left: tempMoveEiL,
                    top: tempMoveEiT,
                    width: tempMoveEiW,
                    height: _getStateLoad.height+118
                },500, 'easeInOutQuint', function(){
                    if(tempEiBgH && $("div[class$=popBg]").height() > $.fn.popUp.DocHeight){
                        $("div[class$=popBg]").height($.fn.popUp.DocHeight);
                    }
                    if(tempEiBgW && $("div[class$=popBg]").width() > $.fn.popUp.DocWidth){
                        $("div[class$=popBg]").width($.fn.popUp.DocWidth);
                    }
                    $.scrollTo({
                        top: tempMoveEiT-5,
                        left: tempMoveEiL
                    }, 500);
                    $("div[class$=LargeImg]").find('center').fadeTo(300, 1.0);
                });
            }else{$.fn.popUp.loadIeFix();}
		}, 1);
    };
    jQuery.fn.popUp.IeFirstLargeImgFix = function(){
        
        window.setTimeout(function() {
            var _getStateFirst = document.getElementById($("div[@class$='popCont']").find('.LargeImg').find('center').find('img').attr('id').toString());
            
            if(_getStateFirst.complete == true){
                var $sIeWidth = null;
                if(_getStateFirst.width > 630){
                    $sIeWidth = _getStateFirst.width;
                    $("div[@class$='popCont']").css('width', $sIeWidth);
                }else{
                    $("div[@class$='popCont']").css('width', 630);
                }
            
                $("div[@class$='popCont']").css({
                    left: ($(document).width()-$("div[@class$='popCont']").width())/2,
                    top: ($(document).height()-$("div[@class$='popCont']").height())/2
                });
                if($("div[@class$='popCont']").height()+15 > $(document).height()){
                    $thisBg.height($("div[@class$='popCont']").height()+15);
                }
                if($sIeWidth > $.fn.popUp.DocWidth){
                    $thisBg.width($sIeWidth+40);
                }
                $.scrollTo({
                    top: ($(document).height()-$("div[@class$='popCont']").height())/2-5,
                    left: ($(document).width()-$("div[@class$='popCont']").width())/2
                }, 500);
            }else{$.fn.popUp.IeFirstLargeImgFix();}
		}, 1);
    };
    jQuery.fn.popUp.ImagePath = '';
    jQuery.fn.popUp.SelectedImgPath = '';
    jQuery.fn.popUp.DocHeight = '';
    jQuery.fn.popUp.DocWidth = '';
    jQuery.fn.popUp.SrcollId = '';
    jQuery.fn.popUp.dimension = function(){};
})(jQuery);
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
