var PopUp;

//Default er lagt inn som kontroll, så denne koden er flyttet inn dit pga id :)
//$(document).ready(function(){
    //initMenuButtons();
    //initSearchInput();
//});

function comboBoxPositioning()
{
    var list = $('.rcbSlide');
    if(list.parent()[0].id != 'form1')
    {
        list.css('display','none');
    }
}

function commentInput(element, mouseevent, helptext)
{    
    if(element.value == helptext && mouseevent == 'focus')
    {
        element.value = '';
        element.style.color = '#817664';
    }
    if(element.value == '' && mouseevent == 'blur')
    {    
        element.value = helptext
        element.style.color = '#E3E0DC';
    }
}

function initSearchInput()
{
    var s = $('#SearchInput');
    s.bind('focus', function(){
        if(s.attr('value') == 'S\u00D8K')
            s.attr('value','');
    });
    s.bind('blur', function(){
        if(s.attr('value') == '')
            s.attr('value','S\u00D8K');
    });
}

function setFlowerFormClickValues(item, helptext)
{
    item.bind('focus', function(){
        if(item.attr('value') == helptext)
            item.attr('value', '');
    });
    item.bind('blur', function(){
        if(item.attr('value') == '')
            item.attr('value', helptext);
    });
}

function initFlowerForm(culture)
{
    var name = $('.FlowerWrap .Form input:eq(0)');
    var post = $('.FlowerWrap .Form input:eq(1)');
    var phone = $('.FlowerWrap .Form input:eq(2)');
    var email = $('.FlowerWrap .Form input:eq(3)');
    var deceased = $('.FlowerWrap .Form input:eq(4)');
    var ceremonyplace = $('.FlowerWrap .Form input:eq(5)');
    var ceremonydate = $('.FlowerWrap .Form input:eq(6)');
    var ceremonytime = $('.FlowerWrap .Form input:eq(7)');
    var ribbontext = $('.FlowerWrap .Form input:eq(8)');
    
    var ribbonfrom = $('.FlowerWrap .Form textarea:eq(0)');
    var cardtext = $('.FlowerWrap .Form textarea:eq(1)');
    var othertext = $('.FlowerWrap .Form textarea:eq(2)');
    
    if(culture == "en")
    {
        setFlowerFormClickValues(name, '* Your first and last name');
        setFlowerFormClickValues(post, '* Your postal address');
        setFlowerFormClickValues(phone, '* Your phone number');
        setFlowerFormClickValues(email, '* Your e-mail');
        setFlowerFormClickValues(deceased, '* Deceased\'s name');
        setFlowerFormClickValues(ceremonyplace, '* Ceremony location');
        setFlowerFormClickValues(ceremonydate, '* Ceremony date');
        setFlowerFormClickValues(ceremonytime, '* Ceremony time');   
        setFlowerFormClickValues(ribbonfrom, 'Write who the message is from');
        setFlowerFormClickValues(ribbontext, 'Write a message here');
        setFlowerFormClickValues(cardtext, 'Write a card message here');
        setFlowerFormClickValues(othertext, 'Other comments');        
    }
    else
    {
        setFlowerFormClickValues(name, '* Ditt fornavn og etternavn');
        setFlowerFormClickValues(post, '* Din postadresse');
        setFlowerFormClickValues(phone, '* Ditt telefonnummer');
        setFlowerFormClickValues(email, '* Din e-post');
        setFlowerFormClickValues(deceased, '* Avd\u00F8des navn');
        setFlowerFormClickValues(ceremonyplace, '* Seremonisted');
        setFlowerFormClickValues(ceremonydate, '* Seremonidato');
        setFlowerFormClickValues(ceremonytime, '* Seremonitid');   
        setFlowerFormClickValues(ribbonfrom, 'Skriv hvem hilsenen er i fra her');
        setFlowerFormClickValues(ribbontext, 'Skriv en hilsen her');
        setFlowerFormClickValues(cardtext, 'Skriv en kort-hilsen her');
        setFlowerFormClickValues(othertext, 'Andre kommentarer');
    }    
}
            
function ComboBoxOpen(sender, eventArgs)
{
    var dropdown = $('div.rcbSlide');
    dropdown.css('backgroundImage', 'url(gfx/combobox_shadow.gif)');
}

//Default er lagt inn som kontroll, så denne koden er flyttet inn dit pga id :)
//function initMenuButtons()
//{
//    var myList = $('#LeftContentWrap ul.MenuList');
//    myList.hide();
//    var menuButtons = $('#LeftContentWrap a.MenuButton.Violet');
//    menuButtons.each(function(index,item){
//        if($(this).hasClass('Clicked'))
//        {
//            $(myList[index]).show();
//        }
//        if($(myList[index]).find('a').length > 0)
//        {
//            $(item).bind('click', function(e){
//                $(myList[index]).slideToggle('normal', function(){
//                    $(item).toggleClass('Clicked');
//                });
//                return false;
//            });
//        }        
//    });
//}

function initProductPopUps()
{
    PopUp = $('div.ProductPopUp');
    $('a.ProductImage').each(function(index,item){
        $(item).bind('click', function(e){
            ProductPopUp(this);
            return false;
        });
    });
}
function initFlowerPopUps()
{
    var flowers = $('a.ProductImage');
    PopUp = $('div.ProductPopUp');
   
    flowers.each(function(index,item){
        $(item).bind('click', function(e){
            FlowerPopUp(this);
            return false;
        });
    });
}
function FlowerPopUp(sender)
{
    
    var sender = $(sender);
    var currentImg = 0;
    
    
    // Get the image-scr, modify it to the larger version and also retrieve the description
    var imgSrc = sender.find('img').attr('src').substring(0,47) + '_628x345.jpg';
    var text = sender.parent().find('div.Bottom').find('span.FlowerCheck label').text();
    $('div.Thumbnails').empty();
    
    // Set image-src and description
    PopUp.find('img:eq(0)').attr('src',imgSrc);
    PopUp.find('.Top .Left').text(text);
    
    // Set placement of popup; middle of ".Middle" and a bit over the thumbnail
    PopUp.css('left', ($('.FlowerWrap').position().left + ($('.FlowerWrap').width() / 2)) - (PopUp.width() / 2) + 'px');
    PopUp.css('top', $(sender).position().top - 100 + 'px');
    
    // If category, retrieve filenames of all images
    var imgTags = $('a.ProductImage img');
    var imgSrcs = $.map(imgTags, function(value){return $(value).attr('src');});
    var thumbArray = $.map(imgSrcs, function(value){return value.replace(/136x136_cropped_/, '90x90_cropped_');});
    var largeSrcs = $.map(imgSrcs, function(value){return value.replace(/136x136_cropped_/, '628x345');});
    // Create thumbnails
    var thumbholder = PopUp.find('.Thumbnails');
    $(thumbArray).each(function(){
        thumbholder.append('<a><img src="' + this + '" /></a>');
        num++;
    });
    
    
        
    // Bind events to forrige and neste buttons
    var forrige = PopUp.find('a[title=Forrige]');
    var neste = PopUp.find('a[title=Neste]');
    var num = -1;
    var thumbs = $('div.Thumbnails a');
    
    // Give first thumbnail border(Active)
    $(thumbs[currentImg]).addClass('Active');
    
    neste.click(function(){
        //var currentImg = $.inArray(PopUp.find('img:eq(0)').attr('src'), imgArray);
        thumbs.eq([currentImg]).removeClass('Active');
        currentImg = (currentImg + 1) % imgSrcs.length;
        
        PopUp.find('img:eq(0)').attr('src', largeSrcs[currentImg]);
        
        thumbs.eq([currentImg]).addClass('Active');
        //PopUp.find('span.Left').text(titleArray[currentImg]);
        
        if(num > 5)
        {
            thumbs.each(function(index,item){
                $(item).replaceWith($(thumbs[index + 1]));
                //thumbs[index].attr('src',thumbs[index + 1]);
            });
        }
        return false;
    });
        
    forrige.click(function(){
        $('div.Thumbnails a').eq([currentImg]).removeClass('Active');
        if(currentImg == 0)
        {
            currentImg = imgSrcs.length - 1;
        }
        else
        {
            currentImg = currentImg - 1;
        }
        PopUp.find('img:eq(0)').attr('src', largeSrcs[currentImg]);
        
        thumbs.eq([currentImg]).addClass('Active');
        PopUp.find('span.Left').text(titleArray[currentImg]);
        return false;
    });
        
    PopUp.find('div.Bottom').css('display','block');
    PopUp.css('height', '518px');
    
    // Set functionality to close-button
    PopUp.find('.Right').one('click', function(e){
        $('div.Thumbnails').empty();
        PopUp.hide();
//        if(forrige)
//            forrige.unbind();
//        if(neste)
//            neste.unbind();
        return false;
    });
    
    // Show pop-up
    PopUp.show();
    
    return false;
}
    
function ProductPopUp(sender)
{
    var sender = $(sender);
    var currentImg = 0;
    
    
    // Get the image-scr, modify it to the larger version and also retrieve the description
    var imgSrc = sender.find('img').attr('src').substring(0,47) + '_628x345.jpg';
    var text = sender.parent().find('a')[1].text;
    $('div.Thumbnails').empty();
    
    // Set image-src and description
    PopUp.find('img:eq(0)').attr('src',imgSrc);
    PopUp.find('.Top .Left').text(text);
    
    // Set placement of popup; middle of ".Middle" and a bit over the thumbnail
    
    PopUp.css('left', ($('.ProductWrap').position().left + ($('.ProductWrap').width() / 2)) - (PopUp.width() / 2) + 'px');
    PopUp.css('top', $(sender).position().top - 100 + 'px');
    
     // If category, retrieve filenames of all images
    if(sender.parent().find('input[type=hidden]').length > 0)
    {
        var imgsAndTitles = sender.parent().find('input[type=hidden]').val().split('|');
        var imgArray = $.map(imgsAndTitles, function(value){return value.split(':')[0];});
        
        var thumbArray = $.map(imgArray, function(value){return value.replace(/628x345/, '90x90_cropped_');});
        var titleArray = $.map(imgsAndTitles, function(value){return value.split(':')[1];});
    
        PopUp.find('div.Bottom').css('display','block');
        PopUp.css('height', '518px');
        
        // Create thumbnails
        var $thumbholder = PopUp.find('.Thumbnails');
    
        // Create an array of thumbnails
        t1 = new Array();
        $(thumbArray).each(function(i,e){
            t1[i] = $('<a href="#"><img src="' + this + '" /></a>');
            if(i<=4)
            {
                $thumbholder.append(t1[i]);
            }
        });
    
        var count = t1.length;
        $thumbs = $(t1);
        // Bind events to forrige and neste buttons
        var forrige = PopUp.find('a[title=Forrige]');
        var neste = PopUp.find('a[title=Neste]');
        thumbs = $('div.Thumbnails a');
    
        // Give first thumbnail border(Active)
        //$(thumbs[0]).addClass('Active');
        $(thumbs).each(function(index,item){
            $(item).click(function(){
                var newSrc = imgArray[index];
                PopUp.find('img:eq(0)').attr('src',newSrc);
                $('div.Thumbnails a[@class=Active]').removeClass('Active');
                $(thumbs[index]).addClass('Active');
                return false;
            });
        });
            
          
        neste.click(function(){
            //currentImg = $.inArray(PopUp.find('img:eq(0)').attr('src'), imgArray);
            
            // Remove border
            $('div.Thumbnails a[@class=Active]').removeClass('Active');
            
            currentImg = (currentImg + 1) % count;
            
            // Set large image
            PopUp.find('img:eq(0)').attr('src', imgArray[currentImg]);
            
            // Set title
            PopUp.find('span.Left').text(titleArray[currentImg]);
            
            // If count is over 5, not all thumbs are shown
            if(count > 5)
            {
                PopUp.find('.Thumbnails').empty();
                
                for(var q = currentImg; q<currentImg+5;q++)
                {
                   var realC = q%count;
                   
                   var clone = $thumbs[realC].clone(true); 
                   $thumbholder.prepend(clone);
                   // Remove border
                   $('div.Thumbnails a[@class=Active]').removeClass('Active');
                   $(thumbs[realC]).addClass('Active');
                }
                
        
                // Re-bind click-event to thumbnails
                // Shouldn't have to be necessary since I've cloned....but alas.....
                thumbs = $('div.Thumbnails a');
                $(thumbs).each(function(index,item){
                    $(this).click(function(){
                        var newSrc = $(this).find('img').attr('src').replace(/90x90_cropped_/, '628x345')
                        PopUp.find('img:eq(0)').attr('src',newSrc);
                        $('div.Thumbnails a[@class=Active]').removeClass('Active');
                        $(thumbs[index]).addClass('Active');
                        return false;
                    });
                });
            }
        else
        {
            $(thumbs[currentImg]).addClass('Active');
        }
        return false;
    });
        
    forrige.click(function(){
        $('div.Thumbnails a').eq([currentImg]).removeClass('Active');
        if(currentImg == 0)
        {
            currentImg = imgSrcs.length - 1;
        }
        else
        {
            currentImg = currentImg - 1;
        }
        PopUp.find('img:eq(0)').attr('src', imgArray[currentImg]);
        
        thumbs.eq([currentImg]).addClass('Active');
        PopUp.find('span.Left').text(titleArray[currentImg]);
        return false;
    });
    
    
    
    }
    
    // Set functionality to close-button
    PopUp.find('.Right').one('click', function(e){
        $('div.Thumbnails').empty();
        PopUp.hide();
        return false;
    });
    
    if(sender.parent().find('input[type=hidden]').length == 0)
    {
        PopUp.find('div.Bottom').css('display','none');
        PopUp.css('height', '380px');
    }
    
    // Show pop-up
    PopUp.show();
    
    return false;
}