isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
$.loadArticle = function(){
        $('#subNav A').click(function(){           
                $("#subNav A").each(function(){
                    $(this).removeClass('on');
                });
                $(this).addClass('on');
                var contHref = jQuery(this).attr('href');
                $.ajax({
                        type: "GET",
                        url: contHref,
                        dataType: "html",
                        beforeSend : function fadeAway(){
                                $('#copy').css({'background':'url(../img/bg/ajax-loader.gif) no-repeat 0 0'});
                                $('#copy').children().animate({'opacity':'0'}, 500);
                        },
                        success: function(cont){
                                var newEntry = $(cont).find('#copy').html();
                                setTimeout(function(){
                                    $('#copy').css({'background-image':'none'});
                                    $('#copy').html(newEntry).children().css({'opacity':'0'}).animate({'opacity':'1'}, 500);
                                    $.imageLoad();
                                 if($('.videos').lenght>0){
                                    $('#cont').height(500);
                                 }else{
                                    $('#copy').jScrollPane({scrollbarWidth:20,showArrows:true}).css('margin',0);
                                    $('.jScrollPaneContainer').css('margin','20px 0 0 40px');
                                 }
                                }, 500);
                        },
                        error: function(e){
                            $('#copy H3').text('Error on loading the content');
                        }
                });
                return false;
        });
}

/*$.loadContent = function(){
        $('#nav A').click(function(){
                var bodyName = jQuery(this).parent().parent().attr('class');
                $("#nav A").each(function(){
                        $(this).removeClass('active');
                        $(this).parent().removeClass('active');
                        $(this).parent().parent().removeClass('active');
                });
                $(this).addClass('active');
                $(this).parent().addClass('active');
                $(this).parent().parent().addClass('active');
                var newCont = jQuery(this).attr('href');
                $.ajax({
                        type: "GET",
                        url: newCont,
                        dataType: "html",
                        beforeSend : function resizeBox(){
                                $('<div />').attr({'class':'loader'}).css({'background':'url(img/bg/ajax-loader.gif) no-repeat 0 0'}).appendTo('#cont');
                                $('#cont').children().animate({'opacity':'0'}, 500);
                        },
                        success: function(cont){
                                var newEntry = $(cont).find('#cont').html();
                                setTimeout(function(){
                                        $('#cont').html(newEntry).children().css({'opacity':'0'}).animate({'opacity':'1'}, 500);
                                        $.loadArticle();
                                        $.imageLoad();
                                        if($('.home #wrapper').length>0){
                                                $('<br />').attr({'class':'cb'}).insertAfter('#logo');
                                                $('#download').appendTo('#footer');
                                        }
                                        $('body').attr({'class':bodyName});
                                        $('.loader').remove();
                                }, 500);
                        },
                        error: function(e){
                            $('#cont H3').html("<h3>Error on loading the content</h3>");
                        }
                });
                return false;
        });
}*/


$.imageLoad = function(){
    curImg=0;
    totImg=0;
    $('#images A').each(function(i,item){
       $(this).attr("id","imageList_"+i);
       var anchorHref = $(this).attr("href");
       var img = new Image();
        $(img).load(function(){
                //image loaded
        })
        .error(function(){
                console.warn("Error while loading image "+anchorHref);
        })
        .attr('src', anchorHref);
        totImg=i+1;
    }) ;   
    $('#images A').click(function(){
            var linkTo = $(this).attr("href");
            var img = new Image();
            curImg = parseInt($(this).attr("id").replace(/imageList_/,""));
            // open overlay
            if($(".overlayContainer").length==0){
                    
                    $("<div></div>")
                            .attr("class","overlayBg")
                            .css({"opacity":0.8,"display":"none"})
                            .prependTo("body")
                            .height($(document).height());                       
                    $("<div></div>")
                            .attr("class","overlayContainer")   
                            .css({"display":"none"})
                            .prependTo("body");
                    }
                    $("<p />")
                        .attr("class","label")
                        .css({color:"#07bbc7","font-size":"1.4em"})
                        .html($(this).find("IMG").attr("alt"))
                        .prependTo(".overlayContainer");
                    $(".overlayBg").height($(document).height());
                    $(".overlayBg").fadeIn("fast",function(){$(".overlayContainer").fadeIn()});                       
                    var img = new Image();
                    $(img).load(function(){
                            $(this).css('display','none');
                            $(this).attr("id","imgContainer").prependTo('.overlayContainer').fadeIn();
                            
                    })
                    .error(function(){
                    })
                    .attr('src', linkTo).css({'border':'3px solid #07bbc7'});
                    
                    $(".overlayContainer").css("margin","0 0 0 -330px");
                 
                        $("<img />")
                            .attr("class","logo")
                            .attr("src","/img/aruna-small.gif")
                            .css({"z-index":"999","float":"left","position":"absolute","left":"-155px","top":"7px"})
                            .prependTo(".overlayContainer");
                    
                   
                    $("<div></div>")
                        .attr("class","close")
                        .css({"z-index":"999","float":"left","position":"absolute","right":"-70px","top":"7px"})
                        .prependTo(".overlayContainer");
                    $("<div />")
                                .attr("id","nextIMG")
                                .css({"z-index":"999","display":"block","position":"absolute","right":"-110px","top":"175px"})
                                .appendTo('.overlayContainer');
                     $("<div />")
                                .attr("id","prevIMG")
                                .css({"z-index":"999","display":"block","position":"absolute","left":"-155px","top":"175px"})
                                .appendTo('.overlayContainer');
                    $("#nextIMG").click(function(){
                        curImg++;
                        if(curImg>=totImg) curImg=0;
                        $(".overlayContainer P.label").hide().html("");
                        $(".overlayContainer IMG#imgContainer").fadeOut(500,function(){
                                $(".overlayContainer IMG#imgContainer").load(function(){
                                        $(this).css('display','none');
                                        $(this).fadeIn(500, function(){
                                                $(".overlayContainer P.label").show().html($("#images A#imageList_"+curImg).find("IMG").attr("alt"));
                                        });
                                })
                                .error(function(){
                                })
                                .attr('src', $("#images A#imageList_"+curImg).attr("href"));
                               
                        });
                    });
                    $("#prevIMG").click(function(){
                        curImg--;
                        if(curImg<0) curImg=totImg-1;
                        $(".overlayContainer P.label").hide().html("");
                        $(".overlayContainer IMG#imgContainer").fadeOut(500,function(){
                                $(".overlayContainer IMG#imgContainer").load(function(){
                                        $(this).css('display','none');
                                        $(this).fadeIn(500, function(){
                                                $(".overlayContainer P.label").show().html($("#images A#imageList_"+curImg).find("IMG").attr("alt"));
                                        });
                                })
                                .error(function(){
                                })
                                .attr('src', $("#images A#imageList_"+curImg).attr("href"));
                               
                        });
                    });
                    
                    $("<a href='#' class='png'></a>")
                        .css({"cursor":"pointer","z-index":"999","float":"left","position":"absolute"})
                        .prependTo(".close");
                    $(".close, .overlayBg").click(function(){
                            $(".overlayContainer").fadeOut("fast",function(){$(".overlayContainer").remove();$(".overlayBg").fadeOut("fast",function(){$(this).remove()})});
                            return false;
                    });
            return false;
    });
}

$.largeVideo = function(){
    /*if(jQuery(".overlayContainer").length==0){
            jQuery("<div />").attr("class","overlayBg").css({"opacity":0.8,"display":"none"}).prependTo("body").height(jQuery(document).height());                       
            jQuery("<div />").attr("class","overlayContainer").css({"display":"none"}).prependTo("body");
            jQuery("<div />").attr("id","flashContent").css({"display":"block"}).prependTo(".overlayContainer");
    }
    jQuery(".overlayBg").height(jQuery(document).height());
    jQuery(".overlayBg").fadeIn("fast",function(){
            jQuery(".overlayContainer").fadeIn();
            var params = { allowScriptAccess: "always", wmode: "transparent", scale: "noscale", base: "/swf/" };
            var flashvars = { serverURL: "", imageOverlay: "/images/{CId}.jpg", passReq: "true" };
            var attributes = {};
            swfobject.embedSWF("/swf/flvPlayer_large.swf", "flashContent", "625", "438", "9.0.0", "/swf/expressinstall.swf", flashvars, params, attributes);
    });
    jQuery(".overlayContainer").css({"margin":"0 0 0 -360px","display":"inline"});
    jQuery("<div />").attr("class","close").css({"z-index":"999","float":"left","position":"absolute","right":"32px","top":"10px"}).prependTo(".overlayContainer");
    setTimeout(function(){
            jQuery("<a href='#' class='png'></a>").css({"cursor":"pointer","z-index":"999","float":"left","position":"absolute"}).prependTo(".close");
    }, 3000);
    jQuery(".close, .overlayBg").click(function(){
            jQuery(".overlayContainer").fadeOut("fast",function(){jQuery(".overlayContainer .close").remove();
            jQuery('.overlayContainer object').fadeOut("fast");
            jQuery(".overlayBg, .overlayContainer").fadeOut("fast")});
            jQuery('#navContent A').removeClass("active");
            return false;
    });*/
    alert('called');
}

$.resizeBg = function(){
            var conf = {},
            totalElements=curList=Animation=windowWidth=windowHeight=centerPosition=0;
            fRatio=1.25;
            autoPlay=true;
            rotationTime=10000;
            $.ArunaBg = {
            Start: function(carousel){
                    this.getWindowWidth();
                    this.getWindowHeight();
                    element=carousel;
                    this.AddCarouselWrapper();
                    this.AddCarousel();
                    this.MoveContent();
                    this.idMore();
                    this.global();
                    this.HoverIndicator();
                    $('#download P A').hide();
                    if(autoPlay) setTimeout(this.MoveElements,5000);   
            },
            getWindowWidth : function(){
                    if (typeof(window.innerWidth) == 'number') {
                            windowWidth = window.innerWidth;
                    }
                    else {
                        if (document.documentElement && document.documentElement.clientWidth) {
                            windowWidth = document.documentElement.clientWidth;
                        }
                        else {
                            if (document.body && document.body.clientWidth) {
                                windowWidth = document.body.clientWidth;
                            }
                        }
                    }
                    return windowWidth;
            },
            getWindowHeight : function(){
                if (typeof(window.innerHeight) == 'number') {
                    windowHeight = window.innerHeight;
                }
                else {
                    if (document.documentElement && document.documentElement.clientHeight) {
                        windowHeight = document.documentElement.clientHeight;
                    }
                    else {
                        if (document.body && document.body.clientHeight) {
                            windowHeight = document.body.clientHeight;
                        }
                    }
                }
                return windowHeight;
            },
            resizeWidth : function(){
                    windowWidth=this.getWindowWidth();
                    /*$('#background').css({'width':windowWidth});
                    $('#carousel IMG').css({'width':windowWidth});*/
                    var leftPos = Math.round((windowWidth-990)/2)-13;
                    $('#carousel IMG').css({'width':'990px'});
            },
            resizeHeight : function(){
                    windowHeight=this.getWindowHeight();
                    //$('#background').css({'height':windowHeight});
                    $('#background').css({'height':'600px'});
                    
            },
            AddCarouselWrapper: function(){
                 hCenter = Math.round(windowWidth-990)/2;
                    hCenter = hCenter>0?hCenter:0;
                    hCenter = hCenter - 8;
                    $("<div />").attr("id","background").prependTo('#wrapper');
            },
            AddCarousel: function(){
                    $("<div />").attr("id","borderTopLeft").prependTo('#background');
                    $("<div />").attr("id","borderTopRight").prependTo('#background');
                    $("<div />").attr("id","borderBottomLeft").prependTo('#background');
                    $("<div />").attr("id","borderBottomRight").prependTo('#background');
                    $("<ul />").attr("id","carousel").prependTo('#background');
            },
            MoveContent: function(){
                    if($('.home #wrapper').length>0){
                            $('#download P A').text('Stop');
                    }
                    $('#download LI A').each(function(i){
                            var img = $(this).attr('href');
                            $('<img />').css({'width':windowWidth}).attr({'src':img}).appendTo('#carousel').wrap('<li />');
                            $(this).attr("id","list_"+i);
                            totalElements=$(this).parent().parent().children().length;                               
                    });
            },
            idMore: function(){
                 
                    var ht = Math.round(windowHeight/3);
                    var wt = Math.round(windowWidth/3);
                    var fifth = Math.round(windowHeight/6);
                    var newIh = Math.round(windowWidth * (1/fRatio));
                    var visible = Math.round(windowHeight);
                    var centerPosition = (newIh - visible)/2;
                    
                    centerPosition=centerPosition>0?centerPosition:0;
                    centerPosition=windowHeight<600?0:centerPosition;

                    hCenter = Math.round(windowWidth-990)/2;
                    
                    hCenter = hCenter>0?hCenter:0;
                    try{
                        var wrapHeight = $('#wrapper').height();
                    } catch(e){
                        alert(e.description);
                    }
                    $('#carousel LI').each(function(i){
                            $(this).attr("id","bg_"+i);
                            $('#carousel LI').css({'position':'absolute','top':'0px',"height":wrapHeight+'px',"width":"990px","left":'0px',overflow:"hidden"});
                            $('#carousel LI IMG').css({"width":"990px"}).attr('width','990').attr('height','539');  // width and height attr added to fix an IE bug
                    });
                    
                    $('#inside').css("padding-top","125px");
                   
                    if($('.home #wrapper').length>0){
                            $('#logo, .home #cont, .home #cont .intro, .home #cont .links, #download').css("width","300px");
                            $('.home #nav, .home #nav UL, .home #nav UL A,').css("width","250px");
                            $('#logo A').css({'background-position':'right 0'});
                            $('.home #wrapper').height(400).css({overflow:'hidden'});
                            
                    }
                    if($('.home #wrapper, .profile #wrapper, .gallery #wrapper, .videos #wrapper, .press #wrapper, .contact #wrapper').length>0){
                            $('#logo').css("width","300px");
                            $('#nav, #nav UL, #subNav, #download').css("width","250px");
                            $('#subNav').css("width","430px");
                            $('#copy').css("width","580px");
                            $('#nav UL LI').css({'float':'right','clear':'both'});
                            $('#logo A').css({'background-position':'right 0'});
                            $('#cont').css({'left':'360px'});
                            var mt = (windowHeight-540)/2;
                            
                            mt = mt>0?mt:0;
                            $('#wrapper').css('margin-top',mt);
                    }
                    $('#cont').css({'top':'30px','min-height':'490px'}); 
                    if($('BODY.profile').length>0 || $('BODY.press').length>0 || $('BODY.gallery').length>0){
                        $('#copy').jScrollPane({scrollbarWidth:20,showArrows:true}).css('margin',0);
                        $('.jScrollPaneContainer').css('margin','20px 0 0 40px');
                    }
                    if($('BODY.contact').length>0){
                         $('#cont').css({'top':'10px','min-height':'530px'}); 
                    }  
                     
                    
            },
            global: function(){
                    $('#carousel LI:first').css({'z-index':'1'});
            },
            MoveElements: function(){
                if($('.home #wrapper').length>0){
                    if(Animation==0){
                            Animation++;
                            $.ArunaBg.GoToNextElement();
                    }
                    setTimeout($.ArunaBg.MoveElements,rotationTime);
                }
            },
            HoverIndicator: function(){
                    if($('.home #wrapper').length>0){
                            $("#download UL LI A").click(function(){
                                    if(Animation==0){
                                        Animation++;
                                        $.ArunaBg.ClickIndicator($(this));
                                    }
                                    $.ArunaBg.ClickIndicator($(this));
                                    return false;
                            });
                            $("#download P A").click(function(){
                                if(Animation==1){
                                        Animation=0;
                                        $("#download P A").text('Stop');
                                        return false;                               
                                } else {
                                    Animation=1;
                                    $("#download P A").text('Change Background');
                                        return false;
                                }  
                            });
                    }
            },
            ClickIndicator: function(click){
                    if($('.home #wrapper').length>0){
                            var newIh = Math.round(windowWidth * (1/fRatio));
                            var visible = Math.round(windowHeight);
                            var centerPosition = (newIh - visible)/2;
                            var listId = click.attr("id").replace(/bg_/,"list_");
                            var listNr = parseInt(click.attr("id").split("_")[1]);
                            if (listNr != curList){
                                    $("#download P A").text('Change Background');
                                    $('#download LI A').each(function(){
                                            $(this).removeClass('active').css({'opacity':'0.5'});
                                    });
                                    click.addClass('active').css({'opacity':'1'});
                                    oldList = curList;
                                    curList = listNr;
                                    $('#carousel LI').each(function(){
                                        $(this).hide();
                                    });
                                    $("#bg_"+(oldList)).css({'opacity':'1','display':'block'}).animate({'opacity':'0'}, 1000);
                                    $("#bg_"+(curList)).css({'opacity':'0','display':'block'}).animate({'opacity':'1'}, 1000, function(){Animation=1;});
                            }
                    }
            },
            GoToNextElement: function(){
                if($('.home #wrapper').length>0){
                            var newIh = Math.round(windowWidth * (1/fRatio));
                            var visible = Math.round(windowHeight);
                            var centerPosition = (newIh - visible)/2;
                            if(Animation<=1){
                                    if((curList+1)<totalElements){
                                            curList++;
                                            $('#carousel LI').each(function(){
                                                $(this).hide();
                                            });
                                            $("#bg_"+(curList-1)).css({'opacity':'1','display':'block'}).animate({'opacity':'0'}, 1000);
                                            $("#bg_"+(curList)).css({'opacity':'0','display':'block'}).animate({'opacity':'1'}, 1000, function(){Animation=0;});
                                            $('#download LI A').each(function(){
                                                $(this).removeClass('active').css({'opacity':'0.5'});
                                            });
                                            $("#list_"+curList).css({'opacity':'0.5'}).animate({'opacity':'1'}, 500, function(){Animation=0;});
                                    } else {
                                            Animation=0;
                                            curList=0;
                                            $("#carousel LI").each(function(){
                                                $(this).hide();
                                            });
                                            $('#download LI:last').removeClass('active').css({'opacity':'0.5'});
                                            $("#list_"+curList).css({'opacity':'0.5'}).animate({'opacity':'1'}, 500, function(){Animation=0;});
                                            $("#carousel LI:last").addClass('on').css({'opacity':'1','display':'block'}).animate({'opacity':'0'}, 1000, function(){Animation=0;});
                                            $("#bg_"+curList).addClass('on').css({'opacity':'0','display':'block'}).animate({'opacity':'1'}, 1000, function(){Animation=0;});
                                    }
                            }
                    }
            }
        };
}

$(document).ready(function(){
    if($('.home #content .links').length>0){
        $(".links a[rel='nofollow']").each(function(){
                $(this).attr("target","_blank");
                $(this).attr("title","This link will open in a new window/tab.");
        });
    }
    
    $('#wrapper').css({'background':'none'});
    $.loadArticle();
    $.imageLoad();
    $.resizeBg();
    
    if($('#wrapper').length>0){
          $.ArunaBg.Start('#background');
    }
    
    if($('#videos').length>0){
        $('#videos LI A').each(function(i){
                var img = $(this).attr('href');
                $(this).attr("id","vid_"+i);
        });
    }
    /*
    $(window).bind('resize', function(){
            $.ArunaBg.resizeWidth();
            $.ArunaBg.resizeHeight();
            $.ArunaBg.idMore();
    });
    */
    
    if($('.home #wrapper').length>0){
            setTimeout(showPage,500);
    }
});

