/*$(document).ready(function(){
    function change_color() {
        $('body').css('background-color', 'red');
        setTimeout(function(){
            $('body').css('background-color', 'green');
        }, 100);
    }
    setInterval(change_color, 200);
});*/



function readyMe() {

    $('.spravka.active .spravka_button').unbind('click').click(function(){
        $(this).parent().parent().removeClass('active').addClass('not_active');
		readyMe();
    });

    $('.spravka.not_active .spravka_button').unbind('click').click( function(){
        $(this).parent().parent().removeClass('not_active').addClass('active');
		readyMe();
    });
}
	



$(document).ready(function(){
	readyMe();
	var slides = $('.slide');
	var slides2 = $('.slide2');
	var slides3 = $('.slide3');
	var numberOfSlides = slides.length;
	var numberOfSlides2 = slides2.length;
	var numberOfSlides3 = slides3.length;
	var currentPosition = 0;
	var currentPosition2 = 0;
	var currentPosition3 = 0;
	var slideWidth = 264;
    // Remove scrollbar in JS
    $('#slidesContainer').css('overflow', 'hidden');
    $('#slidesContainer2').css('overflow', 'hidden');
    $('#slidesContainer3').css('overflow', 'hidden');
	
    // Wrap all .slides with #slideInner div
    slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
    .css({
        'float' : 'left',
        'width' : slideWidth
    });
    
    slides2
    .wrapAll('<div id="slideInner2"></div>')
    // Float left to display horizontally, readjust .slides width
    .css({
        'float' : 'left',
        'width' : slideWidth
    });

    slides3
    .wrapAll('<div id="slideInner3"></div>')
    // Float left to display horizontally, readjust .slides width
    .css({
        'float' : 'left',
        'width' : slideWidth
    });

    // Set #slideInner width equal to total width of all slides
    $('#slideInner').css('width', slideWidth * numberOfSlides);
    $('#slideInner2').css('width', slideWidth * numberOfSlides2);
    $('#slideInner3').css('width', slideWidth * numberOfSlides3);

    // Insert left and right arrow controls in the DOM
    $('#slideshow')
    .prepend('<span class="control" id="leftControl">Move left</span>')
    .append('<span class="control" id="rightControl">Move right</span>');

    $('#slideshow2')
    .prepend('<span class="control2" id="leftControl2">Move left</span>')
    .append('<span class="control2" id="rightControl2">Move right</span>');

    $('#slideshow3')
    .prepend('<span class="control3" id="leftControl3">Move left</span>')
    .append('<span class="control3" id="rightControl3">Move right</span>');

    // Hide left arrow control on first load
    manageControls(currentPosition);
    manageControls2(currentPosition2);
    manageControls3(currentPosition3);
	
    // Create event listeners for .controls clicks
    $('.control')
    .click(function(){
        // Determine new position
        currentPosition = ($(this).attr('id')=='rightControl')
        ? currentPosition+1 : currentPosition-1;

        // Hide / show controls
        manageControls(currentPosition);

        // Move slideInner using margin-left
        $('#slideInner').animate({
            'marginLeft' : slideWidth*(-currentPosition)
        });
    });

    $('.control2')
    .bind('click', function(){
        // Determine new position
        currentPosition2 = ($(this).attr('id')=='rightControl2')
        ? currentPosition2+1 : currentPosition2-1;

        // Hide / show controls
        manageControls2(currentPosition2);
        // Move slideInner using margin-left
        $('#slideInner2').animate({
            'marginLeft' : slideWidth*(-currentPosition2)
        });
    });

    $('.control3')
    .bind('click', function(){
        // Determine new position
        currentPosition3 = ($(this).attr('id')=='rightControl3')
        ? currentPosition3+1 : currentPosition3-1;

        // Hide / show controls
        manageControls3(currentPosition3);
        // Move slideInner using margin-left
        $('#slideInner3').animate({
            'marginLeft' : slideWidth*(-currentPosition3)
        });
    });

// manageControls: Hides and shows controls depending on currentPosition
function manageControls(position){
	// Hide left arrow if position is first slide
	if(position==0){
		$('#leftControl').hide()
	}
	else{
		$('#leftControl').show()
	}
	// Hide right arrow if position is last slide
	if(position==numberOfSlides-1){
		$('#rightControl').hide()
	}
	else{
		$('#rightControl').show()
	}
}

function manageControls2(position){
	// Hide left arrow if position is first slide
	if(position==0){
		
		$('#leftControl2').hide()
	}
	else{
		$('#leftControl2').show()
	}
	// Hide right arrow if position is last slide
	if(position==numberOfSlides2-1){
		$('#rightControl2').hide()
	}
	else{
		$('#rightControl2').show()
	}
}

function manageControls3(position){
	// Hide left arrow if position is first slide
	if(position==0){

		$('#leftControl3').hide()
	}
	else{
		$('#leftControl3').show()
	}
	// Hide right arrow if position is last slide
	if(position==numberOfSlides3-1){
		$('#rightControl3').hide()
	}
	else{
		$('#rightControl3').show()
	}
}
});

function OpenWindow( sUri, iWidth, iHeight ) {
    var sWindowName = 'win' + Math.floor( Math.random()*1000 )
    var iRealWidth = iWidth ? iWidth : 600
    var iRealHeight = iHeight ? iHeight : screen.height - 300
    var iLeft = Math.round( (screen.width-iRealWidth)/2 )
    var iTop = Math.round( (screen.height-iRealHeight)/2 ) - 35
    var sWindowOptions = 'status=yes,menubar=no,toolbar=no'
    sWindowOptions += ',resizable=yes,scrollbars=yes,location=no'
    sWindowOptions += ',width=' + iRealWidth
    sWindowOptions += ',height=' + iRealHeight
    sWindowOptions += ',left=' + iLeft
    sWindowOptions += ',top=' + iTop
    var oWindow = window.open( sUri, sWindowName, sWindowOptions )
    oWindow.focus()
    return oWindow;
}

// Image preload
pic1 = new Image(28,28);
pic1.src = "http://starichki.ru/_themes/main/starichki/img/header/icon_fav_hover.png";

pic2 = new Image(28,28);
pic2.src = "http://starichki.ru/_themes/main/starichki/img/header/icon_friend_hover.png";

pic3 = new Image(28,28);
pic3.src = "http://starichki.ru/_themes/main/starichki/img/header/icon_i_hover.png";

pic4 = new Image(28,28);
pic4.src = "http://starichki.ru/_themes/main/starichki/img/header/icon_qwe_hover.png";

pic5 = new Image(28,28);
pic5.src = "http://starichki.ru/_themes/main/starichki/img/header/icon_env_hover.png";

// Icons hover effect
$('.icon_fav').live('mouseover mouseout', function(event){
    if(event.type == 'mouseover') {
        $(this).attr('src', 'http://starichki.ru/_themes/main/starichki/img/header/icon_fav_hover.png');
    }
    else {
        $(this).attr('src', 'http://starichki.ru/_themes/main/starichki/img/header/icon_fav_still.png');
    }
});

$('.icon_inv').live('mouseover mouseout', function(event){
    if(event.type == 'mouseover') {
        $(this).attr('src', 'http://starichki.ru/_themes/main/starichki/img/header/icon_friend_hover.png');
    }
    else {
        $(this).attr('src', 'http://starichki.ru/_themes/main/starichki/img/header/icon_friend_still.png');
    }
});

$('.icon_faq').live('mouseover mouseout', function(event){
    if(event.type == 'mouseover') {
        $(this).attr('src', 'http://starichki.ru/_themes/main/starichki/img/header/icon_i_hover.png');
    }
    else {
        $(this).attr('src', 'http://starichki.ru/_themes/main/starichki/img/header/icon_i_still.png');
    }
});

$('.icon_help').live('mouseover mouseout', function(event){
    if(event.type == 'mouseover') {
        $(this).attr('src', 'http://starichki.ru/_themes/main/starichki/img/header/icon_qwe_hover.png');
    }
    else {
        $(this).attr('src', 'http://starichki.ru/_themes/main/starichki/img/header/icon_qwe_still.png');
    }
});

$('.icon_contact').live('mouseover mouseout', function(event){
    if(event.type == 'mouseover') {
        $(this).attr('src', 'http://starichki.ru/_themes/main/starichki/img/header/icon_env_hover.png');
    }
    else {
        $(this).attr('src', 'http://starichki.ru/_themes/main/starichki/img/header/icon_env_still.png');
    }
});

// Main menu hover
$('.btm_button_noact').live('mouseover mouseout', function(event){
    if(event.type == 'mouseover') {
        $(this).removeClass('btm_button_noact');
        $(this).addClass('btm_button_act');
    }
});

$('.btm_button_act').live('mouseover mouseout', function(event){
    if(event.type == 'mouseout') {
        $(this).removeClass('btm_button_act');
        $(this).addClass('btm_button_noact');
    }
});

// Sub-menu drop-down
$('.sub_dropdown').live('mouseover mouseout', function(event){
    if(event.type == 'mouseover') {
        $(this).removeClass('not_active');
        $(this).addClass('active');
    }
    else {
        $(this).removeClass('active');
        $(this).addClass('not_active');
    }
});

$('.link_block li[class!=empty]').live('mouseover mouseout', function(event){
    if(event.type == 'mouseover') {
        $(this).css('background-color', '#ffb456');
        $('a', this).css('color', '#ffffff');
    }
    else {
        $(this).css('background-color', '');
        $('a', this).css('color', '');
    }
});
