/**
 * ALL SCRIPTS BY RICHARD CACERES <RICHARD-C.COM><RCACERES@UCLA.EDU>
 * FOR LITTLE MINX <LITTLEMINX.TV>
 * WORKING WITH OSK DESIGN <WWW.OSKDESIGN.COM>
 * APRIL 2010
 */

// vars
var is_in_slideshow_state = false;


/**
* Return the base URL of the script
*
* @return Base URL
*/
function getBaseURL() {
    // Get document base path
    documentBasePath = document.location.href;
    if (documentBasePath.indexOf('?') != -1) {
        documentBasePath = documentBasePath.substring(0, documentBasePath.indexOf('?'));
    }
    if (documentBasePath.indexOf('#') != -1) {
        documentBasePath = documentBasePath.substring(0, documentBasePath.indexOf('#'));
    }
    documentBasePath = documentBasePath.substring(0, documentBasePath.lastIndexOf('/'));
    return documentBasePath;
}
////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// favorites functions
var favorites = new Object();
var favorites_count = 0;
function addFavorite(did, pid, cat) {
    // hide the favorites instructions
    $('#favorites-instructions').hide();

    // ANIMATE :)
    // var duration = 3000;
    // var distance = 250;
    // // calculate the angle to move    
    // var a = $("#dir-" + did + " .favadd").offset();
    // var b = $("#favorites").offset();
    // var c = $("#dir-" + pid + " .favadd").offset();
    // var blen = (b.left - a.left);
    // var alen = (b.top - a.top);
    // var angleR = Math.atan2( alen, blen );
    // var angleD = angleR * 57.29; // rough
    // var deltaL = "+=" + (Math.cos(angleR) * distance);
    // var deltaT = "+=" + (Math.sin(angleR) * distance);
    // 
    // var bg_src = $("#smallthumb-" + pid).css('background-image');
    // var regexp = /url\((.+)\)/;
    // var imgsrc = bg_src.replace(regexp, "$1");
    // //alert(imgsrc);
    // var startleft = a.left;
    // var starttop = a.top;
    // 
    // $("body").append("<div id='fav-anim-"+pid+"'>&nbsp;</div>");
    // $("#fav-anim-" + pid).css({
    //     'position': 'absolute',
    //     'left': startleft + "px",
    //     'top': starttop + "px",
    //     'z-index': '9999',
    //     'width': '125px',
    //     'height': '87px',
    //     'background-image':bg_src,
    //     'background-repeate': 'none'
    // });  
    // $("#fav-anim-" + pid).animate(
    //     { left: deltaL, top: deltaT, opacity: 0.0, width: '125px', height: '87px' }, 
    //     duration,
    //     'easeOutExpo',
    //     function() {$(this).remove();}
    // );

    // make sure its not already added
    var exists = false;
    for (var key in favorites) {
        if (key == String(pid)) {
           exists = true;
           break;   
        }
    }
    if (exists == false) {
        favorites[String(pid)] = did;// add it to memory
        renderFavorite(did, pid, cat);// Render it
        favorites_count++;
        if(favorites_count == 1) {
            $("#favs").fadeOut("normal", function(){
                $(this).html("(" + favorites_count + ")").fadeIn("slow");
            });
        } else {
            $("#favs").html("(" + favorites_count + ")");
        }
    }else{
        //alert('Already in favorites!');   
    }
}
////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function renderFavorite(did, pid) {
    //alert('renderFavorite: ' + pid);
    var turl = getBaseURL() + '/main/partial_favorite_icon.php';    
    $.ajax({
        type: "POST",
        url: turl, 
        data: 'pid='+pid+'&did='+did,
        success: function(html){
            $('#favorites-list').append(html);
        }
    });
}
////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function emailfavorites() {
    var paramstr = '';
    for (var key in favorites) {
        paramstr += key + ':' + favorites[key] + '|';
    }
    var url = getBaseURL() + '?favs=' + encodeURI(paramstr);
    window.location = 'mailto:?subject=Little Minx&body=' + encodeURI(url);
}
////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function loadProject(uid, pid, cat) {
    //alert("loadProject:"+uid+":"+pid+":"+cat);
    // close all players for good measure
    closeAllPlayers();
    // load a project into this directors project box
    var url =  getBaseURL() + '/main/partial_project.php';
    $('.dir-' + uid + ' .projectbox').load(url, {'pid':pid, 'autoplay':'true', 'did':uid, 'cat':cat});
}
////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function showCategory(did, cat) {
    if(cat == '') { 
        return;
    }
    var dirbox = $('.dir-'+did);
    cat_elem = $('[cat='+cat+']', dirbox);
    if(cat_elem.hasClass("active")) {
        return;
    }
    $('.active-category', dirbox).removeClass('active');
    cat_elem.addClass('active');
    
    var url = getBaseURL() + '/main/partial_project_list.php';
    //$('#dir-' + did + ' > .projectlist').html('loading<marquee direction="right">...</marquee>');
    $('.dir-' + did + ' .projectlist').load(url, {'did':did, 'cat':cat }, 
        function(data, textStatus, XMLHttpRequest){
            var firstch = $(".project-li:first", this).children("a:first");
            //firstch.trigger("click");
            loadProject(firstch.attr("dir_uid"), firstch.attr("proj_uid"), firstch.attr("cat"));
        }
    );
}
////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function donothing() {
	// do nothing
}
////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
var movementEnabled = true;
function toggleMovement(){
    movementEnabled = !movementEnabled;
    if(movementEnabled){
        $('#scroll-movement').html("MOVEMENT:ON");
    }else{
        $('#scroll-movement').html("MOVEMENT:OFF");
    }
    return false;
}
////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
var init_hash = "";
$(document).ready(function() {
    // setup scrolling navigation
    $('.linkscroll').click(function(e) {
        var target = String($(this).attr('href'));
        scrollToGrid(target, 1000);
        return false;
    });
    //setup srcoll toggle
    $('#scroll-movement').click(function(e){
        return toggleMovement();
    });
    // change all text areas to have target _blank
    $('.projdesc a').each(function(){
        $(this).attr('target', '_blank');
    });

    // setup gallery pagination (gallery plugin is in other js file)
    $('#gallery-sl').cycle({ 
          next:   '#gallery-next', 
          timeout: 0, // 0 disables auto advance
          pause:   0,
          height: 610,
          speed: 300,
          fx: 'fade' // cover, wipe,fade
    });

    // determine initial action to take
    var baseurl = document.location.href;
    init_hash = window.location.hash;
    if(baseurl.indexOf('favs') > 0) {
        scrollToGrid('#favorites', 0);
    } else if(init_hash.indexOf("/") == 1 && init_hash != "#/index") {
        //console.log("doing this thing");
        scrollToGrid(init_hash, 0);
    } else {
        //console.log("not doing this thing");
        scrollToGrid("#/index", 0);
    }
});
////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
