function slideSwitch() {
    var $active = $('#slideshow img.active');

    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

    // uncomment the 3 lines below to pull the images in random order
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(document).ready(function(){
	// banner slide show
	setInterval("slideSwitch()", 3000);
    
	// navbar login-form
	var _login = $("#login-bar-form");
	if(_login.length > 0)
	{
        var _val;
        var _text1 = _login.find("input[name$='nomember']");
        var _text2 = _login.find("input[name$='fakepassword']");
        var _text3 = _login.find("input[name$='oripassword']");
        
        _text1.bind("focus", function(){
            _val = $(this).val();
            if(_val.toLowerCase() == "nomember"){
                $(this).attr({value: "", style: "color:#000;"});
            }
        });
        _text1.bind("blur", function(){
            if($(this).val() == ""){
                $(this).attr({value: "Nomember", style: "color:#999;"});
            }
        });
        
        _text2.bind("focus", function(){
            _text3.show().focus();
            _text3.css("color", "#000");
            _text2.hide();
        });
        
        _text3.bind("blur", function(){
            if($(this).val() == ""){
                _text2.show();
                _text3.hide();
            }
        });
        
        _login.bind("submit", function(){
            var default_status = $("#login-status").html();
            $("#login-status").html("Loading...");
            
            // send data via ajax
            var data = $(this).serialize();
            $.ajax({
                type: "post", data: data, dataType: "json",
                url: BASE_URL + "ajax_responder/login/check",
                success: function(res){
                    if(res.success == true){
                        parent.location.href = BASE_URL + "excpt/cp/"
                    } else {
                        alert(res.errmsg);
                        $("#login-status").html(default_status);
                        _text1.focus();
                    }
                }
            });
            
            return false;
        });
	}
	
    // lightbox
    $('.lightbox a.lb').lightBox({
        imageLoading: BASE_URL + 'public/images/lightbox/lightbox-ico-loading.gif',
        imageBtnClose: BASE_URL + 'public/images/lightbox/lightbox-btn-close.gif',
        imageBtnPrev: BASE_URL + 'public/images/lightbox/lightbox-btn-prev.gif',
        imageBtnNext: BASE_URL + 'public/images/lightbox/lightbox-btn-next.gif',
        imageBlank: BASE_URL + 'public/images/lightbox/lightbox-blank.gif'
    });
    
    // box-modal
    // testy modal
    $("#insert-testy").bind("click", function(){
        $(".box-modal").modal();
        return false;
    });
    // submit form
    $(".insert-testy").find("form").bind("submit", function(){
        $("#testy-status").append(PRELOADER).append("Loading...");
        var tpl = {
            setup: function(data){
                var d  = '<li><div class="left mr10" style="width: 45px; height: 45px;">';
                    d += '<img src="' + data.pics + '" alt="' + data.name + '" />';
                    d += '</div><div class="left w75"><span class="bold">' + data.name + '</span>. <span class="tanggal">';
                    d += data.date + '</span><br />' + data.comment + '</div>';
                    d += '<div class="clear"></div></li>';
                
                return d;
            }
        }
        
        var data = $(this).serialize();
        $.ajax({
            type: "post", url: BASE_URL + "ajax_responder/testimony/save",
            data: data, dataType: "json",
            success: function(res){
                $("#testy-status").html("");
                if(res.success == true){
                    $.modal.close();
                    
                    var ct = $("ul.testimony");
                    var el = tpl.setup(res.data);
                    ct.find("li:last").remove();
                    $(el).insertBefore(ct.find("li:first"));
                } else {
                    alert(res.errmsg);
                }
            }
        });
        
        return false;
    });
    
    // SELECT NAV
    // album select
    $("#select-gallery-album").bind("change", function(){
        var opt = $(this).find("option:selected").val();
        if(opt != "#") parent.location.href = BASE_URL + "gallery/"+ opt;
    });
    // category select
    $("#select-product-category").bind("change", function(){
        var opt = $(this).find("option:selected").val();
        if(opt != "#") parent.location.href = BASE_URL + "product/"+ opt;
    });
});

