$(function(){
    $(window).bind('load', function() {
        imgGallery();
        imgRotator();
        setH();
        cleanLayout();
        lightbox();
        captcha();
        validateForm();
        navInfo();
        formValidation();
    });
});

function navInfo() {
    w=$('#box').height()-$('#header').height();
    $('#navInfo').height(w);$('#navInfo span').width(w);
}
function captcha() {
    $('#newCaptcha').click(function () {
        $("#captcha").attr("src",'http://'+window.location.hostname+'/addons/captcha/securimage_show.php?' + Math.random());
        return false;
    });
}
function lightbox() {
    $('#bigGallery a.lightbox').lightBox();
}
function cleanLayout() {
    $('#headerImgBox').corner("30px top");
}
function imgGallery() {
    $('#thumbs img').css('cursor','pointer');
    $('#thumbs img').click(function(){
        $('#thumbs img').css('cursor','pointer');
        $(this).css('cursor','auto')
        src=$(this).attr("src").replace("/0/", "/1/");
        $('#imgBig img').attr("src",src);
        if($(this).attr("title") == '') title='&nbsp;'; else title=$(this).attr("title");
        $('p#caption').html(title);
        return false;
    });
}
function imgRotator() {
    var $rotator=$('#imgRotatorList');
    $rotator.wrap('<div></div>');
    var $wrapper=$rotator.parent('div');
    $wrapper.css({'position':'relative','overflow':'hidden','width':'100%','height':'100%'});
    $rotator.children('li').css({'position':'absolute', 'z-index':'0', 'top':'0px'});
    var $top=$rotator.children('li:first');
    $top.css({'z-index':'2'});
    if($rotator.children('li').length > 1){
        var $timer = window.setInterval(function(){
            var $next= $top.next('li');
            $next.css({'z-index':'1'})
            $top.fadeTo(2000,0,function(){
                $top.css({'z-index':'0'}).remove().appendTo($rotator).fadeTo(0,1);
                $next.css({'z-index':'2'})
                $top = $next;
            });
        }, 5000);
    }
}
function validateForm() {
    $('#save').click(function() {
        //alert('works');
        valid = true; selArr = [];
        $('.editIndex select').each(function(i, selected){ 
            if($.inArray($(selected).val(), selArr) > -1) {
                mark($(selected), false); valid = false;
            } else { mark($(selected), true); }
            selArr[i] = $(selected).val();
        });
        $('.req').each(function(){
            if($(this).val() == ''){ mark($(this), false); valid = false;
            } else { mark($(this), true); }
        });
        function mark(obj, pos) {
            if(pos) { obj.css('border','1px solid #D7E0C9');
            } else { obj.css('border','1px solid red'); }
        }
        return valid;
    });
}
function formValidation() {
    $('#sendForm').click(function() {
        var go=true;
        $('.req').each(function(){
            if($(this).val() == ''){ $(this).parent().parent().addClass('invalid'); go = false; } else { $(this).parent().parent().removeClass('invalid'); }
        }); return go;
    });
    $('#prodSaveBtn').click(function() {
        var go=true;
        $('.amountVal').each(function(){
            var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
            var str = $(this).val();
            if(str == '' || numberRegex.test(str)) {
               $(this).removeClass('invalid');
            } else {$(this).addClass('invalid'); go = false;  }
        });return go;
    });
}
function setH() {
    function set(obj,h) {
        obj.each(function(){ if($(this).height() < h) { $(this).height(h); } });
    }
    if($("div#text").length != 0) { set($('#content'),400); } else { set($('#content'),200); }
    //if($("div .editor").length != 0)set($("div .editor"),200);
    set($('#contentWide'),300);
    //set($('#text'),400);
}

