﻿$(document).ready(function() {
    /*$("#vid1").fancybox({
	    'titlePosition'	    : 'over',
	    'transitionIn'		: 'elastic',
	    'transitionOut'		: 'elastic',
	    'overlayColor'		: '#000',
	    'overlayOpacity'	: 0.5
    });*/
    
    $(".fancy").fancybox({
        //'onComplete': function () { $("body").css("overflow", "hidden"); },
	    'titlePosition'	    : 'over',
	    'transitionIn'		: 'elastic',
	    'transitionOut'		: 'elastic',
	    'overlayColor'		: '#000',
	    'overlayOpacity'	: 0.7
    });
    
    var flashvars = 
    {
        file:	"/i/v/proizvodstvo2.flv",
        image:	"/video/aprev.jpg"
    };

    var params =
    {
        allowfullscreen:	'true',
        allowscriptaccess:	'always',
        bgcolor:			'#FFFFFF'
    };
    
    swfobject.embedSWF('/video/player.swf', 'video1', '720', '576', '9.0.0', "expressInstall.swf", flashvars, params);     
    
    startScroll('scroller',2,20);
    
    /*$(".rightScroll").click(function() {$(this).parent().find(".hblock:first-child").animate({ marginLeft: "-=230"}, 500); });
    $(".leftScroll").click(function() {$(this).parent().find(".hblock:first-child").animate({ marginLeft: "+=230"}, 500); });*/
    
    $(".rightScroll").click(function() { scrollPage($(this).parent(), 1); });
    $(".leftScroll").click(function() { scrollPage($(this).parent(), -1); });
    
    $('#nivo').nivoSlider({
        effect:'random', // sliceDown sliceDownLeft sliceUp sliceUpLeft sliceUpDown sliceUpDownLeft fold fade random slideInRight slideInLeft boxRandom boxRain boxRainReverse boxRainGrow boxRainGrowReverse 
        directionNav: false,
        animSpeed:1000,
        pauseTime:6000,
        controlNav: false });
        
    positionProducts();
    
    positionBg();
    $(window).resize(positionBg);
});

function positionProducts()
{
    $("#categories .category .product").mouseover(function(event) { productMouse(event.target, 1); });
    $("#categories .category .product").mouseout(function(event) { productMouse(event.target, 0); }); 
    $("#categories .category .product").click(function(event) { productClick(event.target); });
}

function positionBg()
{
    $("form").css("backgroundImage", "url(/bg.ashx?w=" + $("div#holder").width() + ")");
    //if ($("#wrap").height() + $("#footer").height() > $(window).height())
    if ($("#wrap").height() > $(window).height())
    {
        $("html").css("height", "auto");
        $("body").css("height", "auto");
        $("form").css("height", "auto");
    }
    else    
    {
        $("html").css("height", "100%");
        $("body").css("height", "100%");
        $("form").css("height", "100%");
    }
}

function productMouse(el, over)
{    
    $(el).parent().css("background-color", over ? "#bedbf0" : "#e0e0e0");
}

function productClick(el)
{   
    var _el = $(el).next();
    var prev = _el.parent().parent().parent().prev();
    var lsrc = _el.attr("lsrc");
    prev.find(".largePic").attr("src", _el.attr("lsrc"));
    prev.find("a").attr("href", _el.attr("hsrc"));
}

function startScroll(id,delta,timeout)
{
    var el = $("#" + id);
    el.context.sc_delta = delta;
    el.context.sc_timeout = timeout;
    var twidth = 0;
    el.children().each(function() 
        { 
            var cel = $(this);
            //cel.css("left", twidth + "px");
            cel.css("left", "0px");
            cel.context.sc_left = twidth;
            twidth += cel.width();
        });
    el.context.sc_width = twidth;
    
    scroll(id);
}

function scroll(id)
{
    var el = $("#" + id);
    el.children().each(function() 
        { 
            var sel = $(this);
            //window.alert(sel.css("left"));
            var sleft = sel.css("left");
            var ileft = parseInt(sleft.match(/[\d\-]+/));
            if (ileft < (0 - (sel.width() + sel.context.sc_left)))
             { 
                ileft += el.context.sc_width;
             }
            var left = (ileft-1) + "px";
            //sel.css("left", left);
            sel.context.style.left = left;
        });
    setTimeout(function() { scroll(id); }, 5);
}

function scrollPage(parent, shift)
{
    cPage = parent.get()[0].cPage;
    if (cPage == null || cPage == undefined) { cPage = 1; }
    tPages = parent.find(".hblock").length;
    
    if (((cPage + shift) == 0) || ((cPage + shift) > tPages))
        return;
        
    parent.find(".holderwide").animate( { left: ((shift == 1) ? "-" : "+") + "=230px" }, 500);
    parent.get()[0].cPage = cPage + shift;
}
