/*
Sitewide JS
*/

/*
generateSprites() & attachNavEvents() based on http://www.alistapart.com/articles/sprites2
*/

function generateSprites(parent, setActive, hoverSpeed, style, bodyIdentifier) {
    var parentClass = $(parent).attr("class");
    if(typeof(bodyIdentifier) == 'object'){
        for (var i in bodyIdentifier) if(bodyIdentifier[i] == $('body').attr("class")) var currentPage = i;
    } else if (bodyIdentifier == "class") var currentPage = $('body').attr("class");
    else var currentPage = $('body').attr("id");
    $(parent).children("li").each(function() {
        var myClass = ($(this).attr("class"));
        attachNavEvents(parent, myClass, setActive, hoverSpeed, style);
        if (currentPage != myClass) {
            $(this).children("a").css({backgroundImage:"none"});
        }
    });
}

function attachNavEvents(parent, myClass, setActive, hoverSpeed, style) {
    $(parent + " ." + myClass).mouseover(function() {
        $(this).append('<div class="nav-' + myClass + '"></div>');
        if (style == "slide") {
            $("div.nav-" + myClass).css({display:"none"}).slideDown(hoverSpeed);
        } else {
            $("div.nav-" + myClass).css({display:"none"}).fadeIn(hoverSpeed);
        }
    }).mouseout(function() {
        if (style == "slide") {
            $("div.nav-" + myClass).slideUp(hoverSpeed, function() {
                $(this).remove();
            });
        } else {
            $("div.nav-" + myClass).fadeOut(hoverSpeed, function() {
                $(this).remove();
            });
        }
    });
    if (setActive) {
        $(parent + " ." + myClass).mousedown(function() {
            $("div.nav-" + myClass).attr("class", "nav-" + myClass + "-click");
        }).mouseup(function() {
            $("div.nav-" + myClass + "-click").attr("class", "nav-" + myClass);
        }).mouseout(function() {
            $("div.nav-" + myClass + "-click").attr("class", "nav-" + myClass);
            if (style == "slide") {
                $("div.nav-" + myClass).slideUp(hoverSpeed, function() {
                    $(this).remove();
                });
            } else {
                $("div.nav-" + myClass).fadeOut(hoverSpeed, function() {
                    $(this).remove();
                });
            }
        });
    }
}

function popUpBlockHandler(openFunction, whatTried){
    $("body").append("<div id=\"dialog\" title=\"Pop-Up Blocker Detected\"><p style=\"width:250px;\">The "+whatTried+" tried to open a new window, but was blocked. Click \"Show Window\" to open.</p></div>");
    $("#dialog").dialog({
        bgiframe: true,
        resizable: false,
        width:300,
        height:200,
        modal: true,
        overlay: {
            backgroundColor: '#000',
            opacity: 0.75
        },
        buttons: {
            Cancel: function() {
                $(this).dialog('close');
            },
            "Show Window": function() {
                openFunction();
                $(this).dialog('close');
            }
        }
    });
}

function launchPopup(thisURL,windowName,width,height,left,top){
    var openFunction=function(){

        evbListen = open("", windowName, "width="+width+",height="+height+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable,left="+left+",top="+top+"");
        if(!evbListen || evbListen.closed || !evbListen.thisBodyId){
            evbListen = window.open(thisURL, windowName, "width="+width+",height="+height+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable,left="+left+",top="+top+"");
        } else evbListen.focus();

        // evbListen = window.open( thisURL,windowName,"width="+width+",height="+height+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable,left="+left+",top="+top+"");
        if((evbListen!=null) && (typeof evbListen!="undefined")){
            evbListen.focus();
            return true;
        }
    }
    successfulOpen=openFunction();
    if(!successfulOpen){
        popUpBlockHandler(openFunction,"\"Listen\" player");
    }
}



/*
Landing JS
*/

function attachQuoteEvents(hoverSpeed){
    if($.browser.msie){
        $(".banner .details").css({"background-image":"none","z-index":"1"}).clone().css({"background":"#000000","opacity":"0.8","z-index":""}).html("").insertAfter(".banner .details");
        $("p.quote").css({"opacity":"0.00001", "color":"#ffffff","background-image":"none","z-index":"1"}).clone().css({"background":"#000000","z-index":""}).html("").addClass("quote_bg").insertAfter("p.quote");
        $("h1").mouseover(function() {
            $(this).css("background-position-y","-42px");
            /* $(this).clone().insertAfter(this).addClass("clone").css({"opacity":"0","background-position-y":"-42px"}).fadeIn(hoverSpeed, function(){
                $("h1").css("background-position-y","-42px");
                $(this).remove();
            }); */
            $("p.quote").stop().fadeTo(hoverSpeed, 0.99999);
            $("p.quote_bg").stop().fadeTo(hoverSpeed, 0.6);
        }).mouseout(function() {
            $(this).css("background-position-y","0");
            /* $(this).clone().insertAfter(this).addClass("clone").css({"opacity":"0","background-position-y":"0"}).fadeIn(hoverSpeed, function(){
                $("h1").css("background-position-y","0");
                $(this).remove();
            }); */
            $("p.quote, div.quote_bg").stop().fadeTo(hoverSpeed, 0.00001);
        });
    }else{
        $("p.quote").css({"opacity":"0.00001", "color":"#ffffff"});
        $("h1").mouseover(function() {
            $("p.quote").stop().fadeTo(hoverSpeed, 0.99999);
        }).mouseout(function() {
            $("p.quote").stop().fadeTo(hoverSpeed, 0.00001);
        });
    }
}



/*
Introduction JS
*/

function initializeIntroduction(){
    generateSprites(".previous_next", true, 300, "fade", { previous: 'page_1', next: 'page_5' });
    $("a").each(function(){
        if($(this).attr("href") == ""){
            $(this).click(function(){
                return false;
            });
        }
    });
}



/*
Gallery Index JS
*/

function initializeGalleryIndex(){
    $("#content li > a").each(function(i){
        $("img", this).addClass("thumb");
        thisImageName = $("img", this).attr("alt");
        $(this).css({zIndex:"2"}).append("<img class=\"bg\" src=\"/images/galleries/index_bg/" + thisImageName + ".jpg\">");
        $("img.bg", this).css({opacity:"0",zIndex:"-1",position:"absolute",left:"0",top:"0",border:"0"});
    });
    $("a").mouseover(function(){
        if($.browser.msie && parseInt($.browser.version) <= 6){
            $("img.thumb", this).css({border:"1px solid #fff",left:"-1px",top:"13px"});
        }
        $("img.bg", this).css({opacity:"0.25"});
    }).mouseout(function(){
        if($.browser.msie && parseInt($.browser.version) <= 6){
            $("img.thumb", this).css({border:"0",left:"0",top:"14px"});
        }
        $("img.bg", this).css({opacity:"0"});
    }).click(function(){
        $("img.bg", this).animate({opacity:"0.5"}, 100, function(){
            $(this).animate({opacity:"0.25"}, 100);
        });
    });
};



/*
Gallery JS
*/

if($.browser.msie){
    flashVersionRequired = "9.0.124";
}else{
    flashVersionRequired = "9";
}
current_section = default_section = "current";
anchors = ["cu","ch","dl"];
sections = ["current","change","download"];
h2_offsets = ["0px","-200px","-400px"];
jQuery.easing.def = "easeInOutQuart";
cu_position = ch_position = false;
if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}
function changeSection(new_section,i,changeSpeed){
    if(changeSpeed === undefined) changeSpeed = 400;
    if(new_section !== current_section){
        if(new_section !== "full"){
            $(".gallery_controls #" + new_section + " h2").css({backgroundPosition:h2_offsets[i] + " -25px",cursor:"default"});
            $(".gallery_controls #" + current_section + " h2").css({backgroundPosition:h2_offsets[sections.indexOf(current_section)] + " 0",cursor:""});
            if(current_section == "current"){
                if(new_section == "change"){
                    $(".gallery_controls #change").animate({top:"-441px"}, changeSpeed, "easeOutExpo");
                }else if(new_section == "download"){
                    $(".gallery_controls #change").animate({top:"-441px"}, changeSpeed, "easeOutExpo");
                    $(".gallery_controls #download").animate({top:"-882px"}, changeSpeed, "easeOutExpo");
                }
            }else if(current_section == "change"){
                if(new_section == "current"){
                    $(".gallery_controls #change").animate({top:"0"}, changeSpeed, "easeOutExpo");
                }else if(new_section == "download"){
                    $(".gallery_controls #download").animate({top:"-882px"}, changeSpeed, "easeOutExpo");
                }
            }else if(current_section == "download"){
                if(new_section == "current"){
                    $(".gallery_controls #change").animate({top:"0"}, changeSpeed, "easeOutExpo");
                    $(".gallery_controls #download").animate({top:"-441px"}, changeSpeed, "easeOutExpo");
                }else if(new_section == "change"){
                    $(".gallery_controls #download").animate({top:"-441px"}, changeSpeed, "easeOutExpo");
                }
            }
            new_href = $(".previous_next .previous a").attr("href");
            if(new_href){
                new_href = new_href.split("#")[0];
                if(new_section !== default_section){
                    new_href = new_href + "#" + anchors[sections.indexOf(new_section)];
                }
                $(".previous_next .previous a").attr("href", new_href);
            }
            new_href = $(".previous_next .next a").attr("href");
            if(new_href){
                new_href = new_href.split("#")[0];
                if(new_section !== default_section){
                    new_href = new_href + "#" + anchors[sections.indexOf(new_section)];
                }
                $(".previous_next .next a").attr("href", new_href);
            }
            current_section = new_section;
        }
    }
}
function modify_px_value(originalValue,desiredChange){
    originalValue=parseInt(originalValue.substring(0,originalValue.length-2))+desiredChange;
    return originalValue+"px";
}
function getGalleryState(){
    return current_section;
}
function setGalleryState(name,control_section,reloadDelay){
    if(window.location.pathname != "/galleries/"+name+"/" && window.location.pathname.slice(0,-21) != "/galleries/"+name+"/"){
        window_location = "/galleries/"+name+"/";
        if(window.location.pathname.slice(-21) == "test_setgallerystate/"){
            window_location += "test_setgallerystate/";
        }
        if(control_section !== default_section){
            window_location += "#" + anchors[sections.indexOf(control_section)];
        }
        reloadDelay = parseInt(reloadDelay);
        if(reloadDelay > 0){
            setTimeout("window.location = window_location;",reloadDelay);
        }else{
            window.location = window_location;
        }
    }else{
        if(control_section !== current_section){
            changeSection(control_section,sections.indexOf(control_section),0);
        }
        window.focus();
    }
}
function checkForImages(this_file_name,this_download_less_than_100ppi,this_download_100ppi,this_download_300ppi){
    return $.ajax({
        async: false,
        type:'GET',
        url:'/js/look_for_downloads/',
        data:{file_name:this_file_name,download_less_than_100ppi:this_download_less_than_100ppi,download_100ppi:this_download_100ppi,download_300ppi:this_download_300ppi}
    }).responseText;
}
function initializeGallery(){
    thisURL = document.location.toString();
    if (thisURL.match("#")) {
        thisAnchor = thisURL.split("#");
        thisAnchor = anchors.indexOf(thisAnchor[1]);
        if(thisAnchor !== -1) changeSection(sections[thisAnchor],thisAnchor,0);
    }else{
        try{
            $(".gallery_controls #" + current_section + " h2").css({backgroundPosition:h2_offsets[sections.indexOf(current_section)] + " -25px",cursor:"default"});
        }catch(err){
            $(".gallery_controls #" + current_section + " h2").css("background-position-y","-25px").css("cursor","default");
        }
    }
    $(".gallery_controls h2").each(function(i){
        $(this).click(function(){
            new_section = $(this).parent().attr("id");
            changeSection(new_section,i,400);
        });
        if($.browser.msie && parseInt($.browser.version) <= 6){
            $(this).mouseover(function(){
                if($(this).parent().attr("id") == "download"){
                    $(this).css("background-position-y","-26px");
                }else{
                    $(this).css("background-position-y","-25px");
                }
            }).mouseout(function(){
                $(this).css("background-position-y","0");
            });
        }
    });
    $("#current .down").click(function(){
        if($(this).attr("class").substring(5) !== "disabled"){
            if(cu_position === false) cu_position = $("#current .gallery_thumbs").css("top");
            if((-1*parseInt(cu_position.substring(0,cu_position.length-2))) + 518 >= $("#current .gallery_thumbs div:last-child").position().top){
                $(this).addClass("disabled");
            }
            cu_position = modify_px_value(cu_position, -319);
            $("#current .gallery_thumbs").stop(true).animate({top:cu_position}, 700, "easeOutExpo");
            $("#current .up").removeClass("disabled");
        }
        return false;
    });
    $("#current .up").click(function(){
        if($(this).attr("class").substring(3) !== "disabled"){
            if(cu_position === false) cu_position = $("#current .gallery_thumbs").css("top");
            if((-1*parseInt(cu_position.substring(0,cu_position.length-2))) <= 331){
                $(this).addClass("disabled");
            }
            cu_position = modify_px_value(cu_position, 319);
            $("#current .gallery_thumbs").stop(true).animate({top:cu_position}, 700, "easeOutExpo");
            $("#current .down").removeClass("disabled");
        }
        return false;
    });
    $("#change .down").click(function(){
        if($(this).attr("class").substring(5) !== "disabled"){
            if(ch_position === false) ch_position = $("#change .gallery_thumbs").css("top");
            if((-1*parseInt(ch_position.substring(0,ch_position.length-2))) + 648 >= $("#change .gallery_thumbs > div:last-child").position().top){
                $(this).addClass("disabled");
            }
            ch_position = modify_px_value(ch_position, -365);
            $("#change .gallery_thumbs").stop(true).animate({top:ch_position}, 700, "easeOutExpo");
            $("#change .up").removeClass("disabled");
        }
        return false;
    });
    $("#change .up").click(function(){
        if($(this).attr("class").substring(3) !== "disabled"){
            if(ch_position === false) ch_position = $("#change .gallery_thumbs").css("top");
            if((-1*parseInt(ch_position.substring(0,ch_position.length-2))) <= 374){
                $(this).addClass("disabled");
            }
            ch_position = modify_px_value(ch_position, 365);
            $("#change .gallery_thumbs").stop(true).animate({top:ch_position}, 700, "easeOutExpo");
            $("#change .down").removeClass("disabled");
        }
        return false;
    });
    $("a").each(function(){
        if($(this).attr("href") == ""){
            $(this).click(function(){
                return false;
            });
        }
    });
    thisGalleryName = thisURL.split("/galleries/");
    if (thisURL.match("#")) {
        thisGalleryName = thisGalleryName[1].split("#");
        thisGalleryName = thisGalleryName[0];
    }else{
        thisGalleryName = thisGalleryName[1];
    }
    if(thisGalleryName.substring(thisGalleryName.length-1,thisGalleryName.length) == "/"){
        thisGalleryName = thisGalleryName.substring(0,thisGalleryName.length-1);
    }
    swfobject.embedSWF("/swf/gallery.swf", "full_screen_view", "200", "25", flashVersionRequired, false, {xml:"/xml/galleries.xml",name:thisGalleryName}, {menu:"false",allowfullscreen:"true",allowscriptaccess:"always"});
    if($.browser.msie && parseInt($.browser.version) <= 6){
        $(".gallery_controls #current a").mouseover(function(){
            $("img", this).css({border:"2px solid #ff9",position:"absolute",left:"12px",top:"13px"});
        }).mouseout(function(){
            $("img", this).css({border:"0",left:"14px",top:"15px"});
        });
        $(".gallery_controls #change a").mouseover(function(){
            $("img", this).css({border:"2px solid #ff9",position:"absolute",left:"-2px",top:"8px"});
        }).mouseout(function(){
            $("img", this).css({border:"0",left:"0",top:"10px"});
        });
    }
    /* $("#download a").each(function(){
        thisHref = $(this).attr("href");
        newHref = thisHref.substring(0,thisHref.length-13);
        $(this).attr("href",newHref+".jpg");
        $(this).click(function(){
            //s=document.createElement('script');
            //s.setAttribute('src',newHref+'/');
            //s.setAttribute('type','text/javascript');
            //document.getElementsByTagName('head')[0].appendChild(s);
            //$.getScript(newHref+"/");
            // $("body").load(newHref+"/", false, "return true;");
            // return false;
            $.ajax({
                async: false,
                type:"GET",
                url:newHref+"/"
            });
        });
    }); */
}



/*
Listen JS
*/

function initializeListen(){
    swfobject.embedSWF("/swf/listenStandalone.swf", "listen_player", "235", "331", "9.0.0", false, {xml:"/xml/listen.xml"},{allowscriptaccess:"always"});
}



$(document).ready(function(){
    $(".navigation .listen a, #site_map a#listen").click(function(){
        launchPopup("/listen/","listen_window",235,317,(screen.width-235),20);
        return false;
    });
    generateSprites(".navigation", true, 200, "fade", "id");
    thisBodyId = $("body").attr("id");
    if(thisBodyId=="index"){
        attachQuoteEvents(2000);
    }else if(thisBodyId=="introduction"){
        initializeIntroduction();
    }else if(thisBodyId=="galleries"){
        initializeGalleryIndex();
    }else if(thisBodyId=="gallery"){
        initializeGallery();
    }else if(thisBodyId=="listen"){
        initializeListen();
    }
});