/* init feature slideshow */

var interval;
var current;
var previous;
var slides;

$(function () {

    var length = $('div.slide').length;
    for (var i = 0; i < length; ++i) {
        $('div#slide_navigation').append('<a href="javascript:showSlide(' + i + ');"' + (!i ? ' class="active"' : '') + '></a>');
    }

    interval = setInterval(nextSlide, 10000);
    current = 0;
    prevoius = 0;
    slides = $('div.slide');

    slides.hide();
    slides.eq(0).show();

    function nextSlide() {
        showSlide(current == slides.length - 1 ? 0 : current + 1, false);
    }
});

function showSlide(no, clear) {
    clear = clear === undefined ? true : false;
    previous = current;
    current = no;
    slides.eq(previous).fadeOut(function () {
        $('#slide_navigation a.active').removeClass('active');
        $('#slide_navigation a').eq(no).addClass('active');
        slides.eq(current).fadeIn();
    });

    if (clear) clearInterval(interval);
}


/*
function initThumbs(section, largeImg){
var thumbs = $('div#'+section+'_box div.box_thumbs img');
var selected = largeImg[0];
	
thumbs.mouseover(function() {
for(var i=0, length=thumbs.length; i<length; ++i) {
var thumb = $(thumbs[i]);
thumb.parent().removeClass('active');
if(this == thumbs[i]) {
selected = largeImg[i];
thumb.parent().addClass('active');
$('div#'+section+'_box div.box_main_image img').attr('src', largeImg[i]);
}
}
});
	
}
*/
function restrictMaxLength(e, el) {
    switch (e.keyCode) {
        case 37: // left
            return true;
        case 38: // up
            return true;
        case 39: // right
            return true;
        case 40: // down
            return true;
        case 8: // backspace
            return true;
        case 46: // delete
            return true;
        case 27: // escape
            el.value = '';
            return true;
    }
    return (el.value.length < el.getAttribute("TAMaxLength"));
}

function checkMaxLength(e, el) {
    if (el.value.length <= el.getAttribute("TAMaxLength"));
}


function parseURL(url) {
    var a = document.createElement('a');
    a.href = url;
    return {
        source: url,
        protocol: a.protocol.replace(':', ''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function () {
            var ret = {},
                seg = a.search.replace(/^\?/, '').split('&'),
                len = seg.length, i = 0, s;
            for (; i < len; i++) {
                if (!seg[i]) { continue; }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [, ''])[1],
        hash: a.hash.replace('#', ''),
        path: a.pathname.replace(/^([^\/])/, '/$1'),
        relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [, ''])[1],
        segments: a.pathname.replace(/^\//, '').split('/')
    };
}

$(document).ready(function () {
    if ($("#tabs").length) {
        $("#tabs").tabs();
    }

    $(".helpPopup").colorbox({ width: "860", height: "570", iframe: true });
    $(".helpPopupMeasures").colorbox({ width: "740", height: "380", iframe: true, scrolling: false });
    $(".helpPopupSubstitutions").colorbox({ width: "740", height: "420", iframe: true, scrolling: false });
    $(".reviewPopup").colorbox({ width: "650", height: "500", iframe: true, escKey: false, overlayClose: false });
    $(".tafPopup").colorbox({ width: "600", height: "450", iframe: true, scrolling: false });


    $(".nutritionUrl").click(function () {
        var url = parseURL($(this).attr("href"));
        var width = parseInt(url.params["Width"]) + 50;
        var height = parseInt(url.params["Height"]) + 160;
        $(".nutritionUrl").colorbox({ iframe: true, innerWidth: width, innerHeight: height, scrolling: false });
    });



});

