function mycarousel_initCallback(carousel) {
    $('.jcarousel-control a').bind('click', function() {
        carousel.scroll((jQuery.jcarousel.intval(jQuery(this).text())-1)*4+1);
        $('.jcarousel-control .current').removeClass('current');
        $(this).addClass('current');
        return false;
    });
};

function mycarousel_setupCallback(carousel) {
};


function mycarousel_buttonCallback(carousel, btn, status)
{
	if (!status)
		$(btn).hide();
	else
		$(btn).show();
}

function calculate_size(cl) {
	if (!cl) cl = 0;
    var w = $(window).width();
    var h = $(window).height();
    var dw = $(document).width();
    var dh = $(document).height();
    var mw = 1000;
    var mh = 590;
    if(w<mw) {
        $('#global-wrapper').width(mw);
        $('#background-images').width(mw);
        $('.back-image').width(mw);
        $("#big-size").width(mw);
        //$('#background-images img').width(mw);
    } else {
         $('#global-wrapper').width(w);
         $('#background-images').width(w);
         //$('#background-images img').width(w);
         $('.back-image').width(w);
 	     $("#big-size").width(w);
         if (cl == 1)
         	$('#content-layer').width($(window).width() - 106);
    };
    var ch = $('body').find('.content-wrap').height() + 85;
    if(h<mh) {
        var nh = mh;
        if(ch>h){
            nh = ch;
        }
    } else {
        var nh = h;
        if(ch>h){
            nh = ch;
        }
    };
    $('#content-layer').height(nh);
    nh = $('#content-layer').height();
    $('#global-wrapper').height(nh);
    $('#background-images').height(nh);
    $('#back-image').height(nh);
    $('aside').height(nh);

	$("#big-size").css("height", nh + "px");
	$("#big-size").css("overflow", "hidden");
};

function resize_content() {
    var ww = $(window).width();
    var mw = 1000;
    if(ww<mw) {
        var w = mw;
    } else {
        var w = ww;
    }
    $('#content-layer').width(w-106);
}

function resize_images() {
    var w = $(window).width();
    var h = $(window).height();
    //var w = $('#background-images').width();
    //var h = $('#background-images').height();
    if(w<1000) {
        $('.back-image').width(1000);
    } else {
        $('.back-image').width(w);
    };
    if(h<590) {
        $('.back-image').height(590);
    } else {
        $('.back-image').height(h);
    }
}


function in_array(needle, haystack, strict) {
    var found = false, key, strict = !!strict;
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
}
return found;
}


function open_content() {
    var speed = 0;
    var hr = g_REQUEST_URI;//document.location.href; // поправить по нормальному
    var myArray = ['/index.php', '/portfolio/sites/mir_castle/','/contacts/','/about/','/services/sites/'];
    if (in_array(hr, myArray))
        speed = 1000;

    var ww = $(window).width();
    var mw = 1000;
    var mh = 590;
    if (typeof work_scroll_to != 'undefined') {
    	var wc = work_scroll_to;
    }

    if(ww<mw) {
        var w = mw;
    } else {
        var w = ww;
    };
    $('#content-layer').animate({
        width:w-106
    },
    speed,
    'linear',
    function(){
        $('.fade').fadeIn(speed/2,function(){
            calculate_size(1);
        });
        $('body').find('#work-carousel ul').jcarousel({
            visible:4,
            scroll:4,
            initCallback: mycarousel_initCallback,
            setupCallback: mycarousel_setupCallback,
            buttonNextCallback: mycarousel_buttonCallback,
            buttonPrevCallback: mycarousel_buttonCallback
        });
    	if (typeof wc != 'undefined') {
    		if (wc != 0) {
    			var interval = Math.min(Math.max((wc+1)*200, 3000), 5000);
	    		setTimeout(function(){
		    		carousel = $('body').find('#work-carousel ul').data('jcarousel');
		    		carousel.set_animation(interval);
		    		carousel.scroll(wc+1, true);
		    		carousel.set_animation('normal');
	    		}, 500);
    		}
    	}
        calculate_size(1);
    });
	//$("#work-carousel .jcarousel-prev").hide();
    if (typeof jcount != 'undefined') {
    	if (jcount < 5) {
    		$("#work-carousel .jcarousel-next").hide();
    	}
    }
    setTimeout(function(){
    	calculate_size(1);
    }, 100);
}

function reloadCapcha(){
    $.ajax({type: "POST",
            url: "/_ajax/reload_captcha.php",
            dataType: "json",
            data: "GETCAPCHA=Y",
            success: function(data, textStatus){
                $.each(data, function(key, val) {
                    switch (key) {
                        case "captcha_sid":
                            $(".captcha_sid").val(val);
                            break;
                        case "capchaimg":
                            $(".capchaimg").attr("src",val);
                            break;
                    }
                });
            }
        });
}

$(document).ready(function(){

    $('.bs-pager a').click(function(){
       alert('23');
    });
    if($('body').attr('id') !== 'homepage'){
        open_content();
        $('#main-navigation li a').click(function(){
            $(this).addClass('goto');
            if($(this).parent().hasClass('active')) {
                return false;
            } else {
                $('.fade').fadeOut(500,function(){
                    $('#content-layer').animate({
                        width:0
                    },
                    1000,
                    'linear',
                    function(){
                        var link = $('#main-navigation .goto').attr('href');
                        window.location = link;
                    });
                });
                return false;
            }
        });
    };

    $('#time').jclock();

    $('#lang').hover(
        function(){
            $(this).addClass('opened');
            $(this).find('span').not('.act').show();
        },
        function(){
            $(this).removeClass('opened');
            $(this).find('span').not('.act').hide();
        }
    );
    $('#lang span').click(function(){
        if($(this).hasClass('act'))
        {
            return false;
        }
        else
        {
            $('#lang span.act').removeClass('act');
            $(this).addClass('act');
        }
    });


    $('#main-carousel').cycle({
        fx:'fade',
        speed:500,
        timeout:15000,
        startingSlide: $.cookie("bg_image_id")==null ? 0 : $.cookie("bg_image_id"),
        pager:'#main-carousel-pager',
        next:'#main-c-next',
        prev:'#main-c-prev'
    });

    $('#main-carousel-pager a').live('click',function(){
        var bgimage = $('#main-carousel div:eq(' + $(this).index() + ')').css('background-image');
        $.cookie("bg_image_id", $(this).index());
        $.cookie("bg_image", bgimage);

	    $('#main-carousel').cycle('pause');
	});

    $(window).resize(function(){
        resize_content();
        if($('body').attr('id') !== 'homepage'){
            resize_content();
        } else {
            resize_images();
        }
        calculate_size(1);
        setTimeout(function(){calculate_size(1);}, 100);
    });

    $('#work-carousel li a').mouseenter(
        function(){
            $(this).find('span').remove();
            var title = $(this).children('img').attr('alt');
            $(this).append('<span>'+title+'</span>');
        }
    );

    //let's make closing button'
    $('body').find('.close, #logo-link').bind('click',function(){
        //at first we should hide content
        $('.fade').fadeOut(500,function(){
            //after fading we animate content layer width to 0px
            $('#content-layer').animate({
                width:0
            },
            1000,
            'linear',
            function(){
                //and then we can go to the homepage
                window.location = '/index.php';
            });
        });
        return false;
    });

    $('body').find('#ws-image').cycle({
        fx:'fade',
        speed:500,
        timeout:5000,
        pager:'#ws-pager'
    });

    $('#ws-pager a').live('click',function(){
	    $('#ws-image').cycle('pause');
	});

    $('body').find('#ws-image').mouseenter(function(){
        $('.loop').fadeIn('fast');
    });
    $('body').find('.loop').mouseleave(function(){
        $(this).fadeOut('fast');
    });

    $('body').find('.loop').bind('click',function(){
	    $('#ws-image').cycle('pause');
        var w = $(window).width();
        var h = $(window).height();
        $('#big-size').width(w).height(h).fadeIn();
        var indexOpen = $('#ws-image img:visible').index();
        setBorderFor(indexOpen, 50)
        $('#big-preview').cycle({
            fx:'fade',
            timeout:0,
            startingSlide: $('#ws-image img:visible').index(),
            speed:500,
            prev:'.bp-prev',
            next:'.bp-next',
            pager: '.bs-pager',
            onPrevNextEvent: function(isNext, zeroBasedSlideIndex, slideElement){
                setBorderFor(zeroBasedSlideIndex, 220);
            },
            onPagerEvent: function(zeroBasedSlideIndex, slideElement) {
                setBorderFor(zeroBasedSlideIndex, 50);
            }
        });
    });

    $('body').find('.bp-close').bind('click',function(){
        $('#big-size').fadeOut();
        $('#big-preview').cycle('destroy');
    });

    $('body').find('#big-preview-wrap').bind('click',function(){
        return false;
    });

    $('body').find('#big-size').bind('click',function(){
        $('#big-size').fadeOut();
        $('#big-preview').cycle('destroy');
    });


    $('body').find('.bp-close-old').bind('click',function(){
        $('#big-size').fadeOut();
    });

    $('body').find('#open-map').bind('click',function(){
        //var w = $(window).width();
        //var h = $(window).height();
        $('#big-size').css('z-index','auto').css('opacity','1');
        //$('#big-size').width(w).height(h).fadeIn();
        $('#big-size').fadeIn();
	});


});

function setBorderFor(indexOpen, ti){
    var wi = $('#big-preview img:eq('+indexOpen+')').width();
    var hi = $('#big-preview img:eq('+indexOpen+')').height();

    $('#big-preview-wrap').animate({width: wi + 10},ti);
    $('.rightramka').animate({width: wi + 5, height: hi + 10},ti);
    $('.frame').animate({width: wi - 4, height: hi},ti);
    $('.ugol').animate({height: hi+10},ti);
}


function ShowBigImage(src, desc, w, h)
{
	$("#big-preview img").attr("src", src);
	$("#big-preview-wrap").find(".desc p").html(desc);
	var sw = $(window).width();
	var sh = $(window).height();
	$('#big-size').width(sw).height(sh).fadeIn();

	w = parseInt(w);
	h = parseInt(h);
    $('#big-preview-wrap').animate({width: w + 10}, 50);
    $('.rightramka').animate({width: w + 5, height: h + 10}, 50);
    $('.frame').animate({width: w - 4, height: h}, 50);
    $('.ugol').animate({height: h + 10}, 50);


}
