var imgDir = "/i/";                                                     //folder with controls' images

$(document).ready(function(){
    $("input.textbox").controls('textbox_l.png', 'textbox_r.png');
    $(".button").controls('button_l.png', 'button_r.png');
    $("select.select").each( function(){ $(this).selectStyle() } );
    $("input.textbox").each( function(){ $(this).input_value() } );

    $("table.tariff td:last-child").css("border-right-width", 0);
    $("table.tariff th:last-child").css("border-right-width", 0);
    $("div.blog_item:last").css("border-bottom-width", 0);
    $("div.left_col ul").each(function(){ $("li:last", this).css("border-bottom-width", "0") });
    $(".delivery").deliveryMarker();
    $(".orders tr:odd").addClass("marked");

    $("#mmenu div.submenu").parents("tr").find("> td > div").each( function(){ $(this).submenu() } );
    $("#rotator > .inner").rotator(8000, 400);                                        //params: timeout, speed
    var preload = [
        'mmenu_over_l.gif',
        'mmenu_over_r.gif',
        'submenu_corners.png',
        'submenu_l.gif',
        'submenu_r.gif'
    ];
});


//----------------------------------------  submenu control
$.fn.submenu = function () {
    var parent = $(this).parent(),
        sub = $(".submenu", this);
    if ($(sub).width() > $(parent).width()) {
        if ( ($("#wrap").offset().left + $("#wrap").width() - $(parent).offset().left - $(sub).width()) > 0 ) {
            $(sub).addClass("submenu_r");
            $("span.under", parent).addClass("under_r");
        } else {
            $(sub).addClass("submenu_l");
            $("span.under", parent).addClass("under_l");
        };
    };

    $(parent)
        .mouseenter(function(){
            $(this).addClass("over");
        })
        .mouseleave(function(){
            $(this).removeClass("over");
        });
}

//----------------------------------------  add pre and post images to textboxes and buttons
$.fn.controls = function (left_img_path, right_img_path) {
    $(this).before('<img class="control" alt="" src="'+imgDir+left_img_path+'"/>');
    $(this).after('<img class="control" alt="" src="'+imgDir+right_img_path+'"/>');
}

// ----------------------------------------  hide/show text of textboxes
$.fn.input_value = function(val) {
    if (!val) {
        val = $(this).attr("title");
    };
    $(this)
        .focus(function(){
            if (this.value == val) {
                this.value = "";
            }
        })
        .blur(function(){
            if (this.value == "") {
                this.value = val;
            }
        });
}

//----------------------------------------  selects style script
$.fn.selectStyle = function() {
  if ($(this).hasClass("select")){
    var id = $(this).attr("id");
    var select = document.getElementById(id);
    var images = ["textbox_l.png", "textbox_r.png"];
    $("<div />", {
                id: id+"_span", "class":"select_span",
                css: {
                    top: select.offsetTop+"px",
                    left: select.offsetLeft+"px",
                    width: select.offsetWidth+2+"px"
                }
        })
        .html("<img class='r' src='"+imgDir+images[1]+"' /><img class='l' src='"+imgDir+images[0]+"' /><span><span></span></span>")
        .insertAfter(this);
    var span = $("div#"+id+"_span span span")
    $(this).css("opacity", 0);
    $(span).text(select.options[select.selectedIndex].text);
    $(select).change( function(){
        $(span).text(select.options[select.selectedIndex].text);
    } );
  };
}

//---------------------------------------- images preload script
$.preloadImages = function(preload){
    $j(document.createElement('img')).bind('load', function(){
        if(preload[0]) this.src = imgDir+preload.shift();
    }).trigger('load');
}

//---------------------------------------- rotator
jQuery.fn.extend({
    rotator: function(timeout, speed, correction){
        var clone = $("td:first", this).clone();
        $("td:last", this).after(clone);

        timeout = timeout ? timeout : 7000;
        speed = speed ? speed : 500;
        correction = correction ? correction : 0;

        var left = $(".r_left", this),
            right = $(".r_right", this),
            content = $(".r_content", this),
            position = parseInt($(content).css("left")),
            maxright = -$(content).width() + $(".r_carrier", this).width() + correction,
            maxleft = -correction,
            width = $(".r_item", content).width() + 2 * correction,
            rotate = true;

        $("#rotator")
            .mouseenter(function() { rotate=false })
            .mouseleave(function() { rotate=true });

        //кнопка вправо
        $(right).css("opacity", 0.7)
                .mouseenter(function(){ $(this).animate({"opacity": 1}, speed/3) })
                .mouseleave(function(){ $(this).animate({"opacity": 0.7}, speed/3) })
                .click(function(){ mooveRight() });

        //кнопка влево
        $(left).css("opacity", 0.7)
                .mouseenter(function(){ $(this).animate({"opacity": 1}, speed/3) })
                .mouseleave(function(){ $(this).animate({"opacity": 0.7}, speed/3) })
                .click(function(){ mooveLeft() });

        //слайдшоу
        var slideshow = setInterval(function() {
            if (rotate) {mooveRight()};
        }, timeout);

        //движение вправо
        function mooveRight() {
                    position -= width;
                    if (position > maxright) {
                        $(content).animate({left: position + "px"}, speed);
                    } else if (position <= maxright) {
                        position = maxright;
                        $(content).animate({left: position + "px"}, speed, function() {
                            position = maxleft;
                            $(content).css("left", position);
                        });
                    };
        }

        //движение влево
        function mooveLeft() {
                    position += width;
                    if (position < maxleft) {
                        $(content).animate({left: position + "px"}, speed);
                    } else if (position == maxleft + width) {
                        position = maxright;
                        $(content).css("left", position);
                        position += width;
                        $(content).animate({left: position + "px"}, speed);
                    } else if (position >= maxleft) {
                        position = maxleft;
                        $(content).animate({left: position + "px"}, speed);
                    };

        }
    }
});

//---------------------------------------- delivery options marker
$.fn.deliveryMarker = function() {
    var markerClass = "marked"
    $("input[type='radio']", this).each(function(){
        mark(this);
        $(this).change(function(){ mark(this) });
    });

    function mark(radio) {
        if (radio.checked) {
            $(radio).parents("table").find("tr."+markerClass).removeClass(markerClass);
            $(radio).parents("tr").addClass(markerClass);
        };
    }
}

function selects_update(id) {
    var select = document.getElementById(id);
	if (select != null) {
    var images = ["textbox_l.png", "textbox_r.png"];
    $("<div />", {
                id: id+"_span", "class":"select_span",
                css: {
                    top: select.offsetTop+"px",
                    left: select.offsetLeft+"px",
                    width: select.offsetWidth+2+"px"
                }
        })
        .html("<img class='r' src='"+imgDir+images[1]+"' /><img class='l' src='"+imgDir+images[0]+"' /><span><span></span></span>")
        .insertAfter(select);
    var span = $("div#"+id+"_span span span")
    $(select).css("opacity", 0);
    $(span).text(select.options[select.selectedIndex].text);
    $(select).change( function(){
        $(span).text(select.options[select.selectedIndex].text);
    } );
	}
}
