// video functions
function LoadVideoBar() {

    var videoBar;
    var options = {
        largeResultSet : false,
        string_allDone : "I've finished watching this",
        horizontal : true,
        autoExecuteList : {
          cycleTime : GSvideoBar.CYCLE_TIME_MEDIUM,
          cycleMode : GSvideoBar.CYCLE_MODE_RANDOM,
          statusRoot : document.getElementById("videoBarStatus"),
          thumbnailSize : GSvideoBar.THUMBNAILS_SMALL,
          executeList : ["tommy emmanuel", "tommy emmanuel CAAS", "tommy emmanuel concert", "tommy emmanuel interview", "tommy emmanuel guitar","tommy emmanuel clinic", "tommy emmanuel live"]
        }
      }

    videoBar = new GSvideoBar(document.getElementById("videoBar"),
                              GSvideoBar.PLAYER_ROOT_FLOATING,
                              options);
    }


function tourDatesScroller() {  
    var increment = 3;
    var nextPage = increment;
    var prevAtZeroCondition = increment*2;
    var prevPage = 0;
    //deco-list
    $('#tourdates-nav').append('<li id="td-loading"></li><li id="td-prev" title="Previous">Previous</li><li id="td-next" title="Next">Next</li>');
    
    $('#td-next').click(function () { 
        $('#td-loading').html('<img src="images/icons/ajax-loader.gif" alt="Loading" />');
        $('#latest-tourdates').load('include/home-tourdates.asp?page=' + nextPage, function(responseText){
                                                                                            
            if (responseText.length < 50) {
                $(this).append('<li>End of results - please view the <a href="/tour-dates/">tour dates page for more dates</a></li>');
                $('#td-loading').html('');
                return;
                }
            nextPage+=increment;
            if (nextPage > prevAtZeroCondition) {
                prevPage+=increment;
            }       
            $('#td-loading').html('');
            //console.log(prevPage + " " + nextPage)

        });
        
        
    });
    
        $('#td-prev').click(function () { 
    $('#td-loading').html('<img src="images/icons/ajax-loader.gif" alt="Loading" />');
        $('#latest-tourdates').load('include/home-tourdates.asp?page=' + prevPage, function(responseText){
            if (responseText.length < 50) {
                $(this).append('<li>End of results - please view the <a href="/tour-dates/">tour dates page for more dates</a></li>');
                $('#td-loading').html('');
                return;
                }               
                                                                                            
            if (nextPage <= prevAtZeroCondition) {
                prevPage = 0;
            } else {
                prevPage-=increment;
            }
            if (nextPage <= increment) {
                nextPage = increment;
            } else {
                nextPage-=increment;
            }           
            $('#td-loading').html('');
//console.log(prevPage + " " + nextPage)
        });

    });
    
    }


// contact form checking
function checkContactForm(){
    if (!document.getElementById('fmcontact')) return;
    $('#fmcontact').submit(function () {                              
            $("#fmcontact li").removeClass('error');
            var errorflag = false;
            var rx_email =  /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;  
            var errors = [];
            errors.push('<p><strong>Please check your form submission</strong>:</p><ul class="error-msg">');

            if ($("#fmcontact #Name").val() == '') {    
                errorflag = true;
                $("#fmcontact #Name").parent().addClass('error');
                errors.push('<li>You have to provide your name</li>');
            }

            if ($("#fmcontact #Details").val() == '') {    
                errorflag = true;
                $("#fmcontact #Details").parent().addClass('error');
                 errors.push('<li>There\'s no message</li>');
            }    
            if ($("#fmcontact #Email").val() == '') {    
                errorflag = true;
                $("#fmcontact #Email").parent().addClass('error');
                 errors.push('<li>Please provide a valid email address so we can get back to you</li>');
            }    
                if ($("#fmcontact #Email").val() != '') {    
                var valid_email = rx_email.test($("#fmcontact #Email").val());
                if (!valid_email) {
                    errorflag = true;
                   $("#fmcontact #Email").parent().addClass('error');
                     errors.push('<li>You must provide a valid email, such as <em>name@domain.com</em></li>');
                    } 
                }    
            errors.push('</ul>');
             var error_content = errors.join('')

            if (errorflag) {
            $("#fmcontact #error-msg").html(error_content);    
            //e.preventDefault();
                return false;
            } else {   
                return true;    
            }                                             
  });
    
}

// guestbook form checking
function checkGuestBookForm(){
    if (!document.getElementById('guestBookForm')) return;
    var maxLimit = 500;
     
     $('#guestMessage').keypress(function () {
            $("#guestBookForm #guestMessage").parent().removeClass('error');
            $("#guestBookForm #error-msg").html("");
            if ($(this).val().length > maxLimit) {                
               var error_content = '<p><strong>Please check your form submission</strong>:</p><ul class="error-msg"><li>Sorry, there\'s a maximum limit of 500 characters. You\'ve entered ' + $(this).val().length + '</li></ul>';
                $("#guestBookForm #error-msg").html(error_content);
                $("#guestBookForm #guestMessage").parent().addClass('error');
            }   
     }); 
    
    $('#guestBookForm').submit(function () {                              
            $("#guestBookForm li").removeClass('error');
        var errorflag = false;
     var errors = [];
     errors.push('<p><strong>Please check your form submission</strong>:</p><ul class="error-msg">');
        

            if ($("#guestBookForm #name").val() == '') {    
                errorflag = true;
                $("#guestBookForm #name").parent().addClass('error');
                errors.push('<li>You have to provide your name</li>');
            }                       

            if ($("#guestBookForm #guestMessage").val() == '') {    
                errorflag = true;
                $("#guestBookForm #guestMessage").parent().addClass('error');
                 errors.push('<li>There\'s no message</li>');
            }  
            
            if ($("#guestBookForm #guestMessage").val().length > maxLimit) {    
                errorflag = true;
                $("#guestBookForm #guestMessage").parent().addClass('error');
                 errors.push('<li>Sorry, you can only enter 500 characters</li>');
            }              
  
            errors.push('</ul>');
             var error_content = errors.join('');

            if (errorflag) {
            $("#guestBookForm #error-msg").html(error_content);    
            //e.preventDefault();
                return false;
            } else {   
                return true;    
            }                                             
  });
    
}

function newsScroller() {   
    var increment = 4;
    var nextPage = increment;
    var prevAtZeroCondition = increment*2;
    var prevPage = 0;
    //deco-list
    $('#news-nav').append('<li id="n-loading"></li><li id="n-prev" title="Previous">Previous</li><li id="n-next" title="Next">Next</li>');
    
    $('#n-next').click(function () { 
        $('#n-loading').html('<img src="images/icons/ajax-loader.gif" alt="Loading" />');
        $('#latest-news').load('include/home-news.asp?page=' + nextPage, function(responseText){
            if (responseText.length < 50) {

                $(this).append('<li><h3>End of News results</h3><p>Please view the <a href="/news/">news page for more news</a></p></li>');
                $('#n-loading').html('');
                return;
                }           
            nextPage+=increment;
            if (nextPage > prevAtZeroCondition) {
                prevPage+=increment;
            }       
            $('#n-loading').html('');
            //console.log(prevPage + " " + nextPage)

        });
        
        
    });
    
        $('#n-prev').click(function () { 
    $('#n-loading').html('<img src="images/icons/ajax-loader.gif" alt="Loading" />');
        $('#latest-news').load('include/home-news.asp?page=' + prevPage, function(responseText){
            if (responseText.length < 50) {
                $(this).append('<li><h3>End of News results</h3><p>Please view the <a href="/news/">news page for more news</a></p></li>');
                $('#n-loading').html('');
                return;
                }                                                                                 
            if (nextPage <= prevAtZeroCondition) {
                prevPage = 0;
            } else {
                prevPage-=increment;
            }
            if (nextPage <= increment) {
                nextPage = increment;
            } else {
                nextPage-=increment;
            }           
            $('#n-loading').html('');
//console.log(prevPage + " " + nextPage)
        });

    });
    
    }


function morenav() {
    $('#te-more-nav a').mouseover(function () { 
        var newClass = $(this).attr('class');
        $('#te-more-nav').removeClass();
        $('#te-more-nav').addClass(newClass);
     });

}


function promotersNav() {
    $('#media-nav').append('<ul><li class="main"><a href="#" class="active">Promoter &amp; media info</a></li><li class="bio"><a href="#">Tommy\'s bio</a></li><li class="images"><a href="#">High res photos</a></li><li class="mmedia"><a href="#">Multimedia</a></li><li class="contacts"><a href="#">Key contacts</a></li></ul>');

    $('#media-nav a').click(function () { 
        var targetId = "#" + $(this).parent().attr('class');
        $('#media-nav a').removeClass('active');
        $(this).addClass('active');
        //console.log(targetId);
        $('#main, #bio, #images, #mmedia, #contacts').hide();
        $(targetId).show();
        return false;
     });

}


// Flickr

var FLICKR_ID = "47764476@N08",
FLICKR_COUNT = 20,
FLICKR_PHOTOS = [];
//http://api.flickr.com/services/feeds/photos_public.gne?id=" + FLICKR_ID + "&format=json&tags=2009
function getFlickrPhotos(id, url, callback){
    callback = callback || function(){};
    $.getJSON(url + "?id=" + id + "&format=json&jsoncallback=?",
    function(data){
        FLICKR_COUNT = (data.items.length < FLICKR_COUNT) ? data.items.length : FLICKR_COUNT;

        for (var i=0; i < FLICKR_COUNT; i++) {

            var _img = data.items[i].media.m;
            var _url = data.items[i].link;
            var _title = data.items[i].title;
            FLICKR_PHOTOS.push({img:_img, url:_url, title: _title });
        }
        setTimeout(function(){fillGallery(callback);$("#link-to-flickr").html("<a href='"+data.link+"' target='_blank'>See more photos</a>");
}, 1000)
    });
}


function fillGallery(callback) {
    $.each(FLICKR_PHOTOS, function(index, value) {
        $("#gallery-photos-list").append("<li><a href='"+value.img+"'><img src='"+turnToThumb(value.img)+"' alt='"+value.title+"' /></a></li>");
        if (index === (FLICKR_COUNT - 1)) callback();
    });
}


function getFlickrHomePagePhotos(){
                // tes photos
                var id = "47764476@N08",
        url = "http://api.flickr.com/services/feeds/photos_public.gne",
        d = new Date();
        
        if ((d.getSeconds() % 2) === 0) {
       
         //fans
               id = "25574156@N00";
        url = "http://api.flickr.com/services/feeds/groups_pool.gne";
       
       } 

   
        
 $.getJSON(url + "?id=" + id + "&format=json&jsoncallback=?",
    function(data){
            if (data.items[0]) {
                $("#flickrImage").append("<a target='_blank' href='"+data.items[0].link+"'><img src='"+turnToThumb(data.items[0].media.m)+"' alt='"+data.items[0].title+"' /></a>");

            } else {
                $("#flickrImageDefault").show();
            }
        
    });

}

function turnToThumb(imgUrl) {
    return imgUrl.replace("_m.jpg", "_t.jpg");
}



// load common functions
$(function() {
 if ($.browser.msie && jQuery.browser.version < 8) document.execCommand("BackgroundImageCache",false,true);
});
