// LOOKBOOKS 2.0 Funtions

ie=document.all?1:0;

// Align Columns / Necessary
function alignColumns () {
    var listing = document.getElementById('listing');
    var divider = document.getElementById('divider');
    var content = document.getElementById('content');
    var overlay = document.getElementById('overlay');


    var listingHeight = listing.offsetHeight;
    var contentHeight = content.offsetHeight;

    //if (listingHeight >= contentHeight) {
     listing.style.height = contentHeight-1+'px';
     divider.style.height = contentHeight+1+'px';
     overlay.style.height = contentHeight-54+'px';
    //}


    /***
    else if (listingHeight < contentHeight){
     listing.style.height = contentHeight-1+'px';
     divider.style.height = contentHeight+1+'px';
     overlay.style.height = contentHeight-54+'px';
    }

    if (ie){
     listing.style.height = contentHeight-1+'px';
     divider.style.height = contentHeight+1+'px';
     overlay.style.height = contentHeight-54+'px';
    }        
    ***/     

}
window.onload=function(){ 

    if(status){
        togglePublish (status) ;
    }        

    if(document.getElementById('listing')){ 
      alignColumns();            
    } 


} 
// Edit Title
function editTitle (id, action) {
     var titleId = document.getElementById('title_'+id);
     var editId = document.getElementById('edit_'+id);
    
     if(titleId && editId && action == 'edit') {
        titleId.style.display = 'none';
        editId.style.display = 'block';
    } else if(titleId && editId && action == 'cancel') {
        titleId.style.display = 'block';
        editId.style.display = 'none';
    }
 }
 
 // Add New Lookbook
function newLookbook (action) {
     var fieldId = document.getElementById('new_lookbook');
     var linkId = document.getElementById('add_new');

     if(fieldId && action == 'add') {
        fieldId.style.display = 'block';
		linkId.className = 'new_lookbook_expanded';
    } else if(fieldId && action == 'cancel') {
        fieldId.style.display = 'none';
		linkId.className = 'new_lookbook_closed';
    }
 }
 
 // Edit Notes
function editNotes (action) {
     var notesForm = document.getElementById('notes_form');
     var notesValue = document.getElementById('notes_value'); 
     var textArea = document.getElementById('editNotesTextArea');
     var tempNotes = document.getElementById('temp_notes').value;
    
     if(action == 'edit') {
        notesValue.style.display = 'none';
        notesForm.style.display = 'block';
        textArea.focus();
    } else if (action == 'cancel') {
        notesValue.style.display = 'block';
        notesForm.style.display = 'none';
        textArea.value = tempNotes;
    }
 }
 
// Toogle Publish/Save mode 
function togglePublish (show) {
     var save = document.getElementById('save_changes');
     var publish = document.getElementById('publish_changes');
     var republish = document.getElementById('republish_changes');
     var required_description = document.getElementById('required_description');
     var required_category = document.getElementById('required_category');
    
     //alert(show);
     if(show == 'private') {
        publish.style.display = 'none';
        save.style.display = 'block';
        required_description.style.display = 'none';
        required_category.style.display = 'none';
    } else if (show == 'public' ) {
        republish.style.display = 'block';
        save.style.display = 'none';
        required_description.style.display = 'inline';
        required_category.style.display = 'inline';
    }
}

// Toogle Publish/Save mode 
function toggleWarning (show) {
     var save = document.getElementById('save_changes');
     var publish = document.getElementById('publish_changes');
     var republish = document.getElementById('republish_changes');
     var required_description = document.getElementById('required_description');
     var required_category = document.getElementById('required_category');
     var warning = document.getElementById('warning');
    
     if(show == 'private') {

        if(status == 'public'){        
            warning.style.display = 'block';
            republish.style.display = 'none';
        } else {
            publish.style.display = 'none';
            warning.style.display = 'none';
        }

        save.style.display = 'block';
        required_description.style.display = 'none';
        required_category.style.display = 'none';

    } else if (show == 'public' ) {

        if(status == 'public'){        
            republish.style.display = 'block';
        } else {
            publish.style.display = 'block';
        }            
        save.style.display = 'none';
        warning.style.display = 'none';
        required_description.style.display = 'inline';
        required_category.style.display = 'inline';
    }
}

function showTip (id, action) { 
    var tip = document.getElementById('tip_'+id);
    
    if(action == 'show') {
        tip.style.display = 'block';
    } else if (action == 'hide' ) {
        tip.style.display = 'none';
    }
}
 
function showTipIframe (id, action) { 
    var tip = document.getElementById('tip_'+id);
    var IfrRef = document.getElementById('DivShim');    

    if(action == 'show') {
        tip.style.display = 'block';
        IfrRef.style.width = tip.offsetWidth - 8;
        IfrRef.style.height = tip.offsetHeight - 8;
        //IfrRef.style.top = tip.style.top;
        //IfrRef.style.left = tip.style.left;
        IfrRef.style.zIndex = tip.style.zIndex - 1;
        IfrRef.style.display = "block";

    } else if (action == 'hide' ) {
        tip.style.display = 'none';
        IfrRef.style.display = "none";
    }
}


function toggleSort (id, action) {  
    var tip = document.getElementById(id);
    
    if(action == 'show') {
        tip.style.display = 'block';
    } else if (action == 'hide' ) {
        tip.style.display = 'none';
    }
} 


// Limit the number of characters per textarea
function textCounter(field, count) {
    var maxlimit = count;
    var textarea = document.getElementById(field);
    if (textarea.value.length > maxlimit) {
        textarea.value = textarea.value.substring(0, maxlimit);
        alert('you have reached the maximum character count');
    }
}



//Position Relativity
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curtop += obj.x;
    return curleft;
}


// Reset Focus


function resetFocus(){
    var boxes = YAHOO.util.Dom.getElementsByClassName('drag_thumbnail');

    for (var x = 0; x < boxes.length; x++) {
        document.getElementById(x+1).childNodes[focusCount].className = "thumbnails";
    }
}



var focusCount = 1;
if (ie){var focusCount = focusCount - 1}

var plusListID;


//Yahoo Functions
function initDD() {


    //cache this
    YAHOO.util.DragDropMgr.useCache = true;


    YAHOO.util.DDList = function(id, sGroup, config) {
        if (id) {
            this.init(id, sGroup, config);
            //this.startPos = YAHOO.util.Dom.getXY( this.getEl() );
            this.initFrame();
        }
    }
    YAHOO.extend(YAHOO.util.DDList, YAHOO.util.DDProxy);


    YAHOO.util.DDListTarget = function(id, sGroup, config) {
        if (id) {
            this.init(id, sGroup, config);
        }
    };
    YAHOO.extend(YAHOO.util.DDListTarget, YAHOO.util.DDTarget);


    var boxes = YAHOO.util.Dom.getElementsByClassName('drag_thumbnail');
    for (var x = 0; x < boxes.length; x++) {
        new YAHOO.util.DDList(boxes[x].id);
    }

    //make the lookbook list a target
    var baskets = YAHOO.util.Dom.getElementsByClassName('list_item');
    for (var i = 0; i < baskets.length; i++) {
        new YAHOO.util.DDTarget(baskets[i].id);
    }


    YAHOO.util.DDList.prototype.startDrag = function(x, y) {

        if(this.getEl().className == 'drag_thumbnail'){

            //the image child
            var domCount = 9;
            if (ie){var domCount = domCount - 2}

            var plusCount = 3;
            if (ie){var plusCount = plusCount - 1}

            var dragEl = this.getDragEl();
            var clickEl = this.getEl();

            imgPath = clickEl.childNodes[focusCount].childNodes[domCount].src;

            YAHOO.util.DDList.prototype.onDragOver = function(e, id) {

                if(YAHOO.util.Dom.get(id).className == 'list_item'){
                    YAHOO.util.Dom.get(id).style.backgroundColor = '#ebf2f8';
                    YAHOO.util.Dom.get(id).childNodes[plusCount].style.display = 'block';
                    //alert(YAHOO.util.Dom.get(id).childNodes[focusCount+2].innerHTML);
                    //dragEl.innerHTML = "<div class='drop_confirm'></div><img src='"+imgPath+"' class='thumb_padding' />";
                }


                if(YAHOO.util.Dom.get(id).className == 'drag_thumbnail'){

                    //the divider child
                    var domCount = 7;
                    if (ie){var domCount = domCount - 1}

                    var fromThumb = this.getEl().childNodes[focusCount].childNodes[domCount-6].value;
                    var toThumb = YAHOO.util.Dom.get(id).childNodes[focusCount].childNodes[domCount-6].value;

                    fromThumb = parseInt(fromThumb);
                    toThumb = parseInt(toThumb);

                    if(fromThumb < toThumb){
                        YAHOO.util.Dom.get(id).childNodes[focusCount].childNodes[domCount].className = 'divider_right';
                    } 

                    if(fromThumb > toThumb){
                        YAHOO.util.Dom.get(id).childNodes[focusCount].childNodes[domCount].className = 'divider_left';
                    } 
                    
                }

            }
            

            YAHOO.util.DDList.prototype.onDragOut = function(e, id) {

                if(YAHOO.util.Dom.get(id).className == 'list_item'){
                    YAHOO.util.Dom.get(id).style.backgroundColor = '#fff';
                    YAHOO.util.Dom.get(id).childNodes[plusCount].style.display = 'none';
                    //dragEl.innerHTML = "<img src='"+imgPath+"' class='thumb_padding' />";
                }

                if(YAHOO.util.Dom.get(id).className == 'drag_thumbnail'){

                    //the divider child
                    var domCount = 7;
                    if (ie){var domCount = domCount - 1}

                    YAHOO.util.Dom.get(id).childNodes[focusCount].childNodes[domCount].className = 'divider_none';

                }
            }
            


            dragEl.innerHTML = "<img src='"+imgPath+"' id='dragImage' class='thumb_padding' />";
            dragEl.style.border = '0';
            dragEl.style.opacity = '.50';
            dragEl.style.filter = 'alpha(opacity=50)';


        }
    }




    YAHOO.util.DDList.prototype.onDrag = function(e, id) {

            //alert(this.getDragEl());
            //window.status=xy[1];

            var xy = YAHOO.util.Event.getXY(e);

            if(xy[1]<300) {

                up("arrange_panel");

            } else if(xy[1]>900) {

                down("arrange_panel");
            } else {
            
                stopScroll("arrange_panel");
            }

    }



    YAHOO.util.DDList.prototype.onDragDrop = function(e, id) {

        //the Lookbook ID
        var domCount = 1;
        var slideCount = 11;
        var editCount = 13;

        if (ie){
            var domCount = domCount - 1;
            var slideCount = slideCount -2;
            var editCount = editCount -2;
        }


        if(YAHOO.util.Dom.get(id).className == 'list_item') {

            document.getElementById('status_wait').style.display = 'block';

        
            var mySlide = this.getEl().childNodes[focusCount].childNodes[slideCount].value;
            //alert('slide: '+mySlide);
            var myLookbook = YAHOO.util.Dom.get(id).childNodes[domCount].value;
            //alert('lookbook: '+myLookbook);

            EventBroker.addSlideToLookbookListener = new AddSlideToLookBookListener();
            EventBroker.addSlideToLookbookListener.addSlideToLookbook(myLookbook, mySlide);


            if (document.getElementById('pageType').value == "thumb"){
                theSlide = this.getEl().id;
                lastClicked = theSlide;
                newFocus = parseInt(theSlide);

            }
    
            if (document.getElementById('pageType').value == "edit"){
                document.getElementById('editLink').value = this.getEl().childNodes[focusCount].childNodes[editCount].value;
            }


        }

        if(YAHOO.util.Dom.get(id).className == 'drag_thumbnail') {


            myLookbook = YAHOO.util.Dom.get(id).childNodes[focusCount].childNodes[domCount+2].value;
    

            if (document.getElementById('pageType').value == "thumb"){
                //number of rows
                pagePos = YAHOO.util.Dom.get(id).childNodes[focusCount].childNodes[domCount+4].value;
                getSlides(myLookbook,this.getEl().id-1,id-1,pagePos);
                lastClicked = id;
                newFocus = parseInt(id);
            }
    
            if (document.getElementById('pageType').value == "edit"){
                //page number

                if (document.getElementById('edit_pageNum').value == ''){
                    document.getElementById('edit_pageNum').value = 1;
                }
                
                pagePos = document.getElementById('edit_pageNum').value;

                getSlides(myLookbook,this.getEl().id-1,id-1,pagePos);
                //alert(this.getEl().childNodes[focusCount].innerHTML);
                document.getElementById('editLink').value = YAHOO.util.Dom.get(id).childNodes[focusCount].childNodes[editCount].value;

                lastClicked = id;
                newFocus = parseInt(id);
            }

            YAHOO.util.Dom.get(id).childNodes[focusCount].childNodes[domCount+6].className = 'divider_none';


            //makeRequest('data.php?droppedItem='+this.getEl().id + '&droppedOn=' + id);
            //alert("dropping:"+this.getEl().id);
            //alert("dropping on:"+id);
        }
    }



    //use this only if scrolling i.e. thumbnail page
    YAHOO.util.DDList.prototype.onInvalidDrop = function(e) {

        if (document.getElementById('pageType').value == "thumb"){
            //the Lookbook ID
            var domCount = 3;
            if (ie){var domCount = domCount - 1}

            myLookbook = this.getEl().childNodes[focusCount].childNodes[domCount].value;

            //number of rows
            myRow = this.getEl().childNodes[focusCount].childNodes[domCount+2].value;

            getSlides(myLookbook,lastClicked,lastClicked,myRow);
            document.body.style.cursor = 'default';
            newFocus = parseInt(lastClicked.childNodes[focusCount].value);
        }
    }


    YAHOO.util.DDList.prototype.onMouseDown = function(e) {
            this.startPos = YAHOO.util.Dom.getXY( this.getEl() );
    }

    YAHOO.util.DDList.prototype.onMouseUp = function(e) {

        if(this.getEl().className == 'drag_thumbnail') {
            YAHOO.util.Dom.setXY(this.getEl().id, this.startPos);
            //this.getEl().style.visibility = 'hidden';
        }
    }




}

var myAnim;

// for scrolling when reaching bottom or top
function up(el) {

    // no cache to get new coordinates
    YAHOO.util.DragDropMgr.useCache = false;

    if (myAnim){
        myAnim.stop();
    }

    myAnim = new YAHOO.util.Scroll(el, { scroll: { by: [0, -3075] } }, 15, YAHOO.util.Easing.easeNone);
    myAnim.animate();

}

function down(el) {


    // no cache to get new coordinates
    YAHOO.util.DragDropMgr.useCache = false;

    if (myAnim){
        myAnim.stop();
    }

    myAnim = new YAHOO.util.Scroll(el, { scroll: { by: [0, 3075] } }, 15, YAHOO.util.Easing.easeNone);
    myAnim.animate();
}


function stopScroll(el) {

    // no cache to get new coordinates
    YAHOO.util.DragDropMgr.useCache = true;

    if (myAnim){
        myAnim.stop();
    }

}


function showConfirm(url,more){

    document.getElementById('overlay').style.display = "block";

    if ((document.getElementById('slideTotal').value == 1) && (document.getElementById('lookbookVisibility').value == "PUBLIC")) {

        document.getElementById('last_slide').style.display = "block";
        document.getElementById('last_slide_buttons').style.paddingLeft = "65px";

    } else {

        document.getElementById('delete_confirm').style.display = "block";
        document.getElementById('ok_delete').href = url;

        if (document.getElementById('pageType').value == "thumb"){
            document.getElementById('ok_delete').href = url+more;
        }

        if (document.getElementById('pageType').value == "edit"){
            document.getElementById('ok_delete').href = url;
        }

    }        
}

function hideConfirm(){
        document.getElementById('overlay').style.display = "none";
        document.getElementById('delete_confirm').style.display = "none";
}

function showError(){

    document.getElementById('overlay').style.display = "block";
    document.getElementById('slide_exists').style.display = "block";
    document.getElementById('slide_exists_buttons').style.paddingLeft = "65px";
}

function hideError(){
        document.getElementById('overlay').style.display = "none";

        if (document.getElementById('slide_exists')){
            document.getElementById('slide_exists').style.display = "none";
        }
        
        if (document.getElementById('last_slide')){
            document.getElementById('last_slide').style.display = "none";
        }
}   




// @name      The Fade Anything Technique
// @namespace http://www.axentric.com/aside/fat/
// @version   1.0-RC1
// @author    Adam Michela

var Fat = {
    make_hex : function (r,g,b) 
    {
        r = r.toString(16); if (r.length == 1) r = '0' + r;
        g = g.toString(16); if (g.length == 1) g = '0' + g;
        b = b.toString(16); if (b.length == 1) b = '0' + b;
        return "#" + r + g + b;
    },
    fade_all : function ()
    {
        var a = document.getElementsByTagName("*");
        for (var i = 0; i < a.length; i++) 
        {
            var o = a[i];
            var r = /fade-?(\w{3,6})?/.exec(o.className);
            if (r)
            {
                if (!r[1]) r[1] = "";
                if (o.id) Fat.fade_element(o.id,null,null,"#"+r[1]);
            }
        }
    },
    fade_element : function (id, fps, duration, from, to) 
    {
        if (!fps) fps = 30;
        if (!duration) duration = 3000;
        if (!from || from=="#") from = "#9eaab7";
        if (!to) to = "#fff";
        //if (!to) to = this.get_bgcolor(id);
        
        var frames = Math.round(fps * (duration / 1000));
        var interval = duration / frames;
        var delay = interval;
        var frame = 0;
        
        if (from.length < 7) from += from.substr(1,3);
        if (to.length < 7) to += to.substr(1,3);
        
        var rf = parseInt(from.substr(1,2),16);
        var gf = parseInt(from.substr(3,2),16);
        var bf = parseInt(from.substr(5,2),16);
        var rt = parseInt(to.substr(1,2),16);
        var gt = parseInt(to.substr(3,2),16);
        var bt = parseInt(to.substr(5,2),16);
        
        var r,g,b,h;
        while (frame < frames)
        {
            r = Math.floor(rf * ((frames-frame)/frames) + rt * (frame/frames));
            g = Math.floor(gf * ((frames-frame)/frames) + gt * (frame/frames));
            b = Math.floor(bf * ((frames-frame)/frames) + bt * (frame/frames));
            h = this.make_hex(r,g,b);
        
            setTimeout("Fat.set_bgcolor('"+id+"','"+h+"')", delay);

            frame++;
            delay = interval * frame; 
            
        }


        setTimeout("Fat.set_bgcolor('"+id+"','"+to+"')", delay);
        setTimeout("Fat.set_default()", delay);
        
    },
    set_default : function (id)
    {
        plusListID.className = 'list_item';
        plusListID.childNodes[focusCount+2].innerHTML = '';
        plusListID.childNodes[focusCount+2].className = 'drop_confirm';
        plusListID.childNodes[focusCount+2].style.display = 'none';
    },
    set_bgcolor : function (id, c)
    {
        var o = document.getElementById(id);
        o.style.backgroundColor = c;
    },
    get_bgcolor : function (id)
    {
        var o = document.getElementById(id);
        while(o)
        {
            var c;
            if (window.getComputedStyle) c = window.getComputedStyle(o,null).getPropertyValue("background-color");
            if (o.currentStyle) c = o.currentStyle.backgroundColor;
            if ((c != "" && c != "transparent") || o.tagName == "BODY") { break; }
            o = o.parentNode;
        }
        if (c == undefined || c == "" || c == "transparent") c = "#FFFFFF";
        var rgb = c.match(/rgb\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/);
        if (rgb) c = this.make_hex(parseInt(rgb[1]),parseInt(rgb[2]),parseInt(rgb[3]));
        return c;
    }
}

