// rewrite how the artists work
document.getElementById('artistForm').getElementsByTagName('select')[0].onchange = function() {if (this.value != '') location.href = '/artist/' + this.value + '.php';};
document.getElementById('artistForm').onsubmit = function() {if (this.getElementsByTagName('select')[0].value != '') location.href = '/artist/' + this.getElementsByTagName('select')[0].value + '.php'; return false};

// rewrite how the moods work
// document.getElementById('moodForm').getElementsByTagName('select')[0].onchange = function() {if (this.value != '') location.href = '/gallery/mood/' + this.value + '.php';};
// document.getElementById('moodForm').onsubmit = function() {if (this.getElementsByTagName('select')[0].value != '') location.href = '/gallery/mood/' + this.getElementsByTagName('select')[0].value + '.php'; return false};

// rewrite how the colours work
// document.getElementById('colourForm').getElementsByTagName('select')[0].onchange = function() {if (this.value != '') location.href = '/gallery/colour/' + this.value + '.php';};
// document.getElementById('colourForm').onsubmit = function() {if (this.getElementsByTagName('select')[0].value != '') location.href = '/gallery/colour/' + this.getElementsByTagName('select')[0].value + '.php'; return false};

// check for faqs to rewrite
if (document.getElementById('faqsForm'))
{
    document.getElementById('faqsForm').getElementsByTagName('select')[0].onchange = function() {if (this.value != '') location.href = '/faqs/' + this.value + '.php';};
    document.getElementById('faqsForm').onsubmit = function() {if (this.getElementsByTagName('select')[0].value != '') location.href = '/faqs/' + this.getElementsByTagName('select')[0].value + '.php'; return false};
}

// set up the image replacements if they exist
if (document.getElementById('thumbsElement'))
{
    // block all clicks
    document.getElementById('pictureAnchor').onclick = function() {return false};
    
    // add a mouse over and out event
    document.getElementById('pictureAnchor').onmouseover = function() 
    {
        // get the xpos
        var xpos = ((384 - parseInt(document.getElementById('pictureImage').hiddenWidth)) / 2) + 10;
        this.style.backgroundPosition = xpos + 'px 10px';
    };
    document.getElementById('pictureAnchor').onmouseout = function() {this.style.backgroundPosition = '0 -30px';};
    
    // get all of the li tags
    var liTags = document.getElementById('thumbsElement').getElementsByTagName('li');
    
    // loop through the li tags to find those that are valid
    for (var i = 0; i < liTags.length; i ++)
    {
        // find the a tag
        var aTag = liTags[i].getElementsByTagName('a');
        
        // if one exists, add an onclick on to it
        if (aTag.length != 0)
        {
            // block all clicks
            aTag[0].onclick = function() {return false};
            
            // add an onclick to change the image
            buffalo.utils.events.add(aTag[0], 'click', swapImage);
        }
    }
    
    // add the first swap image in
    swapImage(false, document.getElementById(document.getElementById('pictureAnchor').rel));
}

// setup the signup box
if (document.getElementById('signupEmail'))
{
    document.getElementById('signupEmail').onclick = function() { if (document.getElementById('signupEmail').value == 'Your Email Address') document.getElementById('signupEmail').value = '';};
}

// find all a tags
var anchors = document.getElementsByTagName('a');
var store   = new Array();

// loop through, storing all those with the correct rel name and img child
for (var i = 0; i < anchors.length; i ++)
    if (anchors[i].rel == 'nectar')
        store[store.length] = anchors[i];

// loop through the store creating the showDisplay links
for (var i = 0; i < store.length; i ++)
{
    store[i].onclick = function() {return false};
    buffalo.utils.events.add(store[i], 'click', function() {nectar.showDisplay({variable:'nectar', image: this.href, title: this.title, margin: 14, barHeight: 32}); return false});
}

// swaps the main image for the given one
function swapImage(mouseEvent, element)
{
    // check for element setup
    if (!element) element = this.parentNode;
    
    // find the data to swap with
    var inputs = element.getElementsByTagName('input');
    var data   = new Array();
    
    // loop through and update as we find
    for (var i = 0; i < inputs.length; i ++)
    {
        // check for straight innerHTML replacement
        if (inputs[i].name == 'pictureTitle' || inputs[i].name == 'pictureDesc' || inputs[i].name == 'picturePrice' || inputs[i].name == 'pictureSize'){
            var d = document.getElementById(inputs[i].name);
            d.innerHTML = inputs[i].value;
        }
        
        // check for value replacement
        if (inputs[i].name == 'pictureId')
            document.getElementById(inputs[i].name).value = inputs[i].value;
        
        // check for the alt replacement
        if (inputs[i].name == 'pictureTitle')
        {
            document.getElementById('pictureAnchor').title  = inputs[i].value;
            document.getElementById('hiddenName').innerHTML = inputs[i].value;
        }
        
        // check for img replacement
        if (inputs[i].name == 'vlargeImage')
            document.getElementById('pictureAnchor').href = inputs[i].value;
            
        // check for img replacement
        if (inputs[i].name == 'largeImage')
            document.getElementById('pictureImage').style.backgroundImage = 'url(' + inputs[i].value + ')';
        
        // check for the height alterations
        if (inputs[i].name == 'pictureHeight')
        {
            document.getElementById('pictureImage').style.height  = inputs[i].value + 'px';
            document.getElementById('pictureAnchor').style.height = inputs[i].value + 'px';
        }
        // check for the height alterations
        if (inputs[i].name == 'pictureWidth')
        {
            document.getElementById('pictureImage').style.backgroundPosition = (((384 - inputs[i].value) / 2) + 10) + 'px 10px';
            document.getElementById('pictureImage').hiddenWidth = inputs[i].value;
            // document.getElementById('pictureAnchor').style.backgroundPosition = (((384 - inputs[i].value) / 2) + 10) + 'px 10px';
        }
        
        // check for the mood
        if (inputs[i].name == 'pictureMood')
        {
            // get the array of the mood
            var mood = inputs[i].value.split('|');
            
            // store the new link
            document.getElementById('pictureMood').innerHTML = '<a href="/gallery/mood/' + mood[0] + '.php" title="' + mood[1] + '">' + mood[1] + '</a>';
        }
        
        // check for the colour
        if (inputs[i].name == 'pictureColours')
        {
            // clear the link
            document.getElementById('pictureColours').innerHTML = '';
            
            // get the array of the colours
            var colours = inputs[i].value.split(',');
            
            // loop through the colours and store
            if (inputs[i].value != '')
                for (var j = 0; j < colours.length; j ++)
                {
                    // get the colour data
                    var colour = colours[j].split('|');
                    
                    // add the spacer in
                    if (j != 0)
                        document.getElementById('pictureColours').innerHTML += ', ';
                    
                    // store the new link
                    document.getElementById('pictureColours').innerHTML += '<a href="/gallery/colour/' + colour[0] + '.php" title="' + colour[1] + '">' + colour[1] + '</a>';
                }
        }
        
        
        // store the data
        data[inputs[i].name] = inputs[i].value;
    }
    
    // check for removals
    if (document.getElementById('pictureAnchor').imageData)
        buffalo.utils.events.remove(document.getElementById('pictureAnchor'), 'click', setNewNectar);
    
    // store the data in the anchor briefly
    document.getElementById('pictureAnchor').imageData = data;
    
    // add an onclick to change the nectar command
    buffalo.utils.events.add(document.getElementById('pictureAnchor'), 'click', setNewNectar);
};

// replace the nectar
function setNewNectar()
{
    // set up the new nectar
    nectar.showDisplay({variable:'nectar', image: this.imageData['vlargeImage'], title: this.imageData['pictureTitle'], margin: 14, barHeight: 32});
    
    // return false
    return false;
};
