// Bahetle-1
// Petr Malakhaltsev (c) 2010
// Main page cards script

var MPCards = new Array();

var stopMoving = 0;
var prevDelta = 0;

$(document).ready(function(){
    
    $('#products').cycle({ 
    fx:      'fade', 
    speed:    1000, 
    timeout:  2000,
    delay:  1000
    });
    
    $('.banner_move').cycle({ 
    fx:      'fade', 
    speed:    300, 
    timeout:  1000,
    delay:  500
    });
    
    $('.banner_move2').cycle({ 
    fx:      'fade', 
    speed:    300, 
    timeout:  500,
    delay:  500
    });
    
    //MPRoundMoveInit();
    /*
    $("#cards div").hover(function() {
        $(this).find('img').css({'z-index' : '10'});
        $(this).find('img').addClass("hover").stop()
        .animate({
        marginTop: '-20px', 
        marginLeft: '-35px', 
        width: '210px', 
        height: '140px',
        padding: '20px'
        }, 200);
      
        } , function() {
        $(this).find('img').css({'z-index' : '1'});
        $(this).find('img').removeClass("hover").stop()
        .animate({
        marginTop: '0', 
        marginLeft: '0',
        width: '150px', 
        height: '100px', 
        padding: '1px'
        }, 400);
        });
    $(".menuitem").click(function(){
        window.location = $(this).find('a').attr('href');
    });
    $(".menuitem").hover(function(){
        $(this).find('img').attr("src","/public/images/menu/active_point.png");
    }, function() {
        $(this).find('img').attr("src","/public/images/menu/point.png");
    });
    $("#products div").hover(function() {
        stopMoving = 1;
        if ((delta==0) && (MouseY>150)){
            $(this).find('img').css({'cursor' : 'pointer'});
            $(this).find('img').css({'z-index' : '10'});
        }
        if ((delta==0) && (MouseY>150))
            $(this).find('img').addClass("hover").stop()
            .animate({
            marginTop: '-20px', 
            marginLeft: '-35px', 
            width: '210px', 
            height: '140px',
            padding: '20px'
            }, 200);
            } , function() {
            stopMoving = 0;
            $(this).find('img').css({'z-index' : '2'});
            $(this).find('img').css({'cursor' : 'auto'});
            $(this).find('img').removeClass("hover").stop()
            .animate({
            marginTop: '0', 
            marginLeft: '0',
            width: '120px', 
            height: '80px', 
            padding: '1px'
            }, 400);
            });
    $("#products div").click(function() {
        if ((delta==0) && (MouseY>150)){
            window.location = '/';
        }
    });
    */
    $("#card-cunpuy").click(function(){
       window.location = "/ru/arendators/view/item/56";
    });
    $("#card-cherry").click(function(){
       window.location = "/ru/arendators/view/item/57";
    });
    $("#card-arendators").click(function(){
       window.location = "/ru/arendators/";
    });
    $("#card-actions").click(function(){
       window.location = "/ru/info/view/category/5";
    });
    $("#card-news").click(function(){
       window.location = "/ru/news/";
    });
    
});

var moving = 0;
var currentK = 1;
var active = 0;
var currentMove = 0;

function mainMove(k){
    
                if (k==1){
                    if (parseInt($("#background").offset().left)>-30){
                        return;
                    }
                }
                else{
                    if ((parseInt($("#background").offset().left)*-1+parseInt($(window).width()))>1570){
                        return;
                    }
                }
    
    currentK = k;
    moving = 1;
    mainMoveDo();
    
}

function mainMoveStop(k){
    
    moving = 0;
    
}

function mainMoveDo(){
    
    if (moving == 1){
        $("#main_space").children().each(function(){
            active++;
            
            var prevLeft = parseInt($(this).css("margin-left"));
            
            if ($(this).attr('id')=='background'){
                if (currentK==1){
                    if (parseInt($(this).offset().left)>-30){
                        moving = 0;
                        currentMove = $(this).offset().left+30;
                    }
                    else
                        currentMove = 30*currentK; 
                }
                else{
                    if ((parseInt($(this).offset().left)*-1+parseInt($(window).width()))>1570){
                        moving = 0;
                        currentMove = 1600-20-(parseInt($(this).offset().left)*-1+parseInt($(window).width()));
                    }
                    else
                        currentMove = 30*currentK; 
                }
            }
            prevLeft = prevLeft + currentMove;
                $(this).animate({"margin-left" : prevLeft+'px'}, {duration: 'normal',
                    complete: function(){
                        active--;
                        if (active==0){
                            mainMoveDo();
                        }
                    }
                    }
                );
        });
    }
    
}

var pi=Math.PI;
var delta = -0.2;

function MPRoundMoveInit(){
    
    var action = new MPRoundMove('product-1',10);
    var action = new MPRoundMove('product-2',60);
    var action = new MPRoundMove('product-3',110);
    var action = new MPRoundMove('product-4',170);
    var action = new MPRoundMove('product-5',220);
    var action = new MPRoundMove('product-6',270);
    var action = new MPRoundMove('product-7',320);
    
}

function MPRoundMove(elementId,alfa){
    
    var self = this;
    self.delta = -0.2;
    self.corner = 0;
    this.alfa = alfa;
    this.beta = 1;
    this.element=document.getElementById(elementId);
    this.x = 0;
    this.y = 0;
    this.radiusX = 320;
    this.radiusY = 180;
    this.corner = 0;
    this.centerX = getClientWidth()/2-60;
    this.centerY = 120;
    this.Go();
    
}

MPRoundMove.prototype.Go = function(){
    
    if  (this.alfa==360 || this.alfa==-360)
        this.alfa=0;
    
    this.element.style.left=this.x+"px";
    this.element.style.top=this.y+"px";
    this.corner=2*pi-this.alfa/180*pi; // переводим градусы в радианы с учетом периода в 2 пи
    this.x = parseInt(this.radiusX*Math.cos(this.corner) + this.centerX);  // считаем новые координаты точки по оси х
    this.y = parseInt((this.radiusY-100)*Math.sin(this.corner) + this.centerY);  // считаем новые координаты по оси у
    this.alfa+=delta;
    var self = this;
    if(this.beta>0)
        setTimeout(function(){ self.Go() },0.5);
    else
        return true;
    
}

function MPOpenWindow(element){
    
    var windowId = element.id+'-window';
    var window = document.getElementById(windowId);
    if (window==undefined)
        return 0;
    window.style.display = 'block';
    
}

function MPCloseWindow(element){
    
    
    
}

var MouseX;
var MouseY;

function defPosition(event) {
    var x = y = 0;
    if (document.attachEvent != null) { // Internet Explorer & Opera
        MouseX = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        MouseY = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    } else if (!document.attachEvent && document.addEventListener) { // Gecko
        MouseX = event.clientX + window.scrollX;
        MouseY = event.clientY + window.scrollY;
    } else {
        // Do nothing
    }
}
document.onmousemove = function(event) {  
    var event = event || window.event;
    defPosition(event);
    if (stopMoving==0){
        if (MouseY<360){
            var realMouse = MouseX-550;
            if (Math.abs(realMouse)>100)
                delta = -1*(realMouse)/1000;
            else
                delta = 0;
        }
        else
            delta = -0.2;
    }
}

function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

