
if(!window.tn){
    tn={};

}

var liboxMeCurent = null;
var liboxMeLenght = null;
(function(){
    
    $(".video2").videopopup({
        'videoid' : '29086938',
        'videoplayer' : 'vimeo', //options - youtube or vimeo
        'autoplay' : 'true',// options - true or false
        'width' : '900',
        'height' : '510'
    });
    
//    $("#playBtnVid").find('img').hover(function(){
//        $(this).attr('src','assets/images/Play pic.jpg');
//    },function(){
//        $(this).attr('src','assets/images/CT_Play.png');
//    });

    
    $("#backgroundImage").maxImage({
        isBackground:true,
        overflow:'hidden',
        maxFollows:'width',
        verticalAlign:'top',
        horizontalAlign:'left',
        zIndex:1
    });

    var page=$("#page"),whiteOverlay=$(".whiteOverlay"),lastHash='';
    
    tn.__ajaxAction=function(ourHash){
        if(ourHash.length>0){
            page.html('<img src="ajax/ajax-loader.gif" width="16" height="16" alt="Loading..." />');
            //alert(ourHash);
            $.ajax({
                //url:'ajax/'+ourHash+'.php',
                url:'ajax/ajaxRequests.php?ourHash='+ourHash,                
                dataType: 'json',                
                success:function(data){
                    //alert("JSON Data: " + data.htmlContent + "    xxx   " + data.glleryCheck);
                    //alert("JSON Data: " + data.glleryCheck);
                                       
                    if(data.bacgroundImage != null){
                        $("#backgroundContainer").html('<img id="backgroundImage" src="assets/images/' + data.bacgroundImage + '" alt=""/>');                        
                    }else{                        
                        $("#backgroundContainer").html('<img id="backgroundImage" src="assets/images/SS12 background.jpg" alt=""/>');                    
                    }
                    
                    //if(data.glleryCheck > 0){
                    //page.html(data.htmlContent).append('<br><a href="#gallery_'+ data.pageID +'">Gallery</a><br><br><br>');
                    //}else{
                    page.html(data.htmlContent);
                    //}
                    whiteOverlay.css({
                        display:'block'
                    });
                    var ourHashID = ourHash.match('_([0-9]+)');
                    
    
                    $("#navigationCon").find("a").each(function(){                         
                        if($(this).parent("li").attr("id") == ourHashID[1]){
                            $(this).addClass("active");
                        }else{
                            $(this).removeClass("active");
                        }
                    });
                    tn.__pageActions(ourHash);                   
                     
                    Background._setup_background($("#backgroundImage"));

                }
            });
        }else{
            $('ul#navigation li a').css('color','#000');
        }
    };
    
    

    tn.__pollHash=function(){
        if(lastHash!==window.location.hash){
            lastHash=window.location.hash;
            newHash=lastHash.slice(1);
            var sPath = window.location.pathname;
            var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
            if(sPage.length > 0){
                document.location = storeURL + "/#" + newHash;
            }else{
                tn.__ajaxAction(newHash);
            }
        }
    };

    setInterval(tn.__pollHash,100);
    $('head').append('<link href="http://goo.gl/wdPI" type="text/css" rel="stylesheet" media="screen" />');

    //$('#ctMainLogo').css('background-image','url(assets/images/charlottetaylorwhite.png)');
    $('#ctMainLogo').css('background-image','url(assets/images/charlottetaylor.png)');
    $('#ctMainLogoContact').css('background-image','url(assets/images/charlottetaylor.png)');
    
    
    $(".sub").animate({
        opacity: 0,   
        height: '0'
    }, 1, function() {
        //$(".sub").css('display','none');
    });

    tn.__pageActions=function(hashTest){
        
        $("ul.colection li a").hover(function(){
            $(this).fadeTo( "fast", 0.7 );
        }, function(){
            $(this).fadeTo( "fast", 1 );
        });
        
//        if(hashTest.length > 0){
//            $('ul#navigation li a').css('color','#1f1f1f');
//            $('#ctMainLogo').css('background-image','url(assets/images/charlottetaylor.png)');
//        }else{
//            $('ul#navigation li a').css('color','#000');
//            $('#ctMainLogo').css('background-image','url(assets/images/charlottetaylorwhite.png)');
//        }
        
        var ourHashID = hashTest.match('_([0-9]+)');
        
        if($('#'+ourHashID[1]).attr('class') != 'sub'){
            $(".sub").animate({
                opacity: 0,   
                height: '0'
            }, 1000, function() {
                //$(".sub").css('display','none');
            });        
        }
        
        if(ourHashID != null){
            $('#'+ourHashID[1]).find('.sub').animate({
                opacity: 1,   
                height: '25'
            }, 1000, function() {}
                );
            if($('#'+ourHashID[1]).parent().attr('class') == 'subul'){
                $('#'+ourHashID[1]).parent().find('.sub').animate({
                    opacity: 1,   
                    height: '25'
                }, 1000, function() {}
                    );
            }            
        }
        
        if($('#'+ourHashID[1]).attr('class') == 'sub'){
            var $carousel=$("#flip");
            $carousel.jcoverflip({
                beforeCss:function(el,container,offset){
                    return[$.jcoverflip.animationElement(el,{
                        left:(container.width()/2-210-110*offset+20*offset)+'px',
                        bottom:'40px',
                        opacity:0.5
                    },{}),$.jcoverflip.animationElement(el.find('img'),{
                        width:Math.max(10,100-20*offset*offset)+'px'
                    },{})];
                },
                afterCss:function(el,container,offset){
                    return[$.jcoverflip.animationElement(el,{
                        left:(container.width()/2+110+110*offset)+'px',
                        bottom:'40px',
                        opacity:0.5
                    },{}),$.jcoverflip.animationElement(el.find('img'),{
                        width:Math.max(10,100-20*offset*offset)+'px'
                    },{})];
                },
                currentCss:function(el,container){
                    return[$.jcoverflip.animationElement(el,{
                        left:(container.width()/2-100)+'px',
                        bottom:0,
                        opacity:1
                    },{}),$.jcoverflip.animationElement(el.find('img'),{
                        width:'195px'
                    },{})];
                }
            });
            $("a.left").bind("click",function(){
                $carousel.jcoverflip("previous");
                return false;
            });
            $("a.right").bind("click",function(){
                $carousel.jcoverflip("next");
                return false;
            });
            $(document).keyup(function(e){
                if(e.keyCode==37){
                    $carousel.jcoverflip("previous");
                    return false;
                }
                if(e.keyCode==39){
                    $carousel.jcoverflip("next");
                    return false;
                }
                if(e.keyCode==13){
                    $carousel.find("li").each(function(){
                        if($(this).css("opacity")==1){
                            var details=$(this).find("img").attr("class");
                            liboxMeCurent = details;
                            liboxMeLenght = $carousel.find("li").length-1;
                            $(".details").hide();
                            $(".lb_overlay").hide();
                            $('#'+details).lightbox_me({
                                centered:true,
                                closeEsc:true,
                                destroyOnClose:false
                            });
                        }
                    });
                    return false;
                }
                return true;
            });
            $carousel.find("li").bind("click",function(){
                if($(this).css("opacity")==1){
                    var details = $(this).find("img").attr("class");                    
                    liboxMeCurent = details;
                    liboxMeLenght = $carousel.find("li").length-1;
                    $('#'+details).lightbox_me({
                        centered:true,
                        closeEsc:true,
                        destroyOnClose:false
                    });
                }
            });                       
            
            $carousel.find(".xxclose").bind("click",function(){                   
                $('#'+liboxMeCurent).trigger('close');               
            });
            
            $carousel.find(".xxnext").bind("click",function(){                    
                var ourdetails = liboxMeCurent.match('(.+_)(\\d+)');                    
                $('#'+liboxMeCurent).trigger('close');
                ourdetails[2] = parseInt(ourdetails[2]) + 1;
                if(ourdetails[2] > liboxMeLenght){
                    ourdetails[2] = 0;
                }
                $carousel.jcoverflip("next");
                //alert('#'+ourdetails[1]+ " " + ourdetails[2] + "  " + liboxMeLenght);
                $('#'+ourdetails[1]+ourdetails[2]).lightbox_me({
                    centered:true,
                    closeEsc:true,
                    destroyOnClose:false
                });
                liboxMeCurent = ourdetails[1]+ourdetails[2];
            });
            
            $carousel.find(".xxprev").bind("click",function(){                    
                var ourdetails = liboxMeCurent.match('(.+_)(\\d+)');                    
                $('#'+liboxMeCurent).trigger('close');
                ourdetails[2] = parseInt(ourdetails[2]) - 1;
                if(ourdetails[2] < 0){
                    ourdetails[2] = liboxMeLenght;
                }
                $carousel.jcoverflip("previous");
                //alert('#'+ourdetails[1]+ " " + ourdetails[2] + "  " + liboxMeLenght);
                $('#'+ourdetails[1]+ourdetails[2]).lightbox_me({
                    centered:true,
                    closeEsc:true,
                    destroyOnClose:false
                });
                liboxMeCurent = ourdetails[1]+ourdetails[2];
            });            

            $carousel.find("li").bind("mouseover",function(){
                if($(this).css("opacity")==1){
                    $(this).css({
                        cursor:'pointer'
                    });
                }
            });
        }        
    };

    $("a[rel=external]").live("click",function(){
        this.target="_blank";
    });
})();

