/*************************************************************************/
/*                                                                       */
/*    project:  GROUP OFFICE web 2010                                    */
/*    code:     servodata_script.js                                      */
/*    author:   Radovan Čapek                                            */
/*    date:     12.11.2010                                               */
/*                                                                       */
/*************************************************************************/
//Edit: Martin Opršal - 21.1.2011
window.onload = function () {
    $('#main_menu ul .active a').addClass('active');
    var submenu = $('#main_menu ul li').index($('#main_menu ul li.active'));
    $('#main_submenu ul').css('display', 'none');
    $('#main_submenu ul:eq(' + submenu + ')').css('display', 'block');
}
//End Edit
var news_changer;
var active_box = 0;
/********************************************************************/
/*     HomePage scripts                                             */
/*     requirements : jQuery.js                                     */
/********************************************************************/
$(document).ready(
/********************************************************************/
/*     HomePage scripts                                             */
/*     requirements : jQuery.js                                     */
/********************************************************************/
    function () {
        //$('#login_window_content').css('display','none');
        //$('#login_first_a').html('<strong>Přihlásit se </strong>');
        //$('#login_window').css('top','-135px'); 
        if ($('#actuallity_subbox').length != 0)
            news_changer = setTimeout(showNews, 12000);
        /********************************************************************/
        /*     script name: Show login window                               */
        /*     describe: change text and show login window                  */
        /********************************************************************/
        //     $('#login_first_a')
        // 			.bind(
        // 				'click',                    // on click
        // 				function(e)
        // 				{
        // 					
        // 					
        //           if($('#login_window_content').css('display') == "none") {
        //           
        //               e.preventDefault();
        //               $('#login_window_content').slideDown();
        //               $('#login_first_a').html('Zapomenuté heslo');
        //           }
        // 				}
        // 			);

        /********************************************************************/
        /*     script name: Show login window                               */
        /*     describe: change text and show login window                  */
        /********************************************************************/
        $('#login_first_a')
 			.bind(
 				'click',                    // on click
 				function (e) {
 				    if ($('#login_window').css('top') == '-145px') {

 				        e.preventDefault();
 				        $('#login_window').animate({
 				            top: '+=145px'
 				        }, 700, function () {
 				            $('#login_window').css('top', '0px');
 				        });
 				        $('#login_first_a').html('Zapomenuté heslo');
 				    } else { //EDIT: Martin Opršal - Allium 28.1.2011
 				        if ($('#LoginFormDiv').css('display') == 'none') {
 				            $('#PasswordRetrievalFormDiv').css('display', 'none');
 				            $('#LoginFormDiv').css('display', 'block');
 				            $('#login_first_a').html('Zapomenuté heslo');
 				        } else {
 				            $('#LoginFormDiv').css('display', 'none');
 				            $('#PasswordRetrievalFormDiv').css('display', 'block');
 				            $('#PasswordRetrievalFormDiv > div').css('display', 'block');
 				            $('#login_first_a').html('<strong>Přihlásit se </strong>');
 				        } //END EDIT
 				    }
 				}
 			);
        /********************************************************************/
        /*     script name: Hide login window                               */
        /*     describe: change text and hide login window                  */
        /********************************************************************/
        $('#login_window')
 			.bind(
 				'mouseleave',                    // on mouseleave
 				function () {
 				    if ($('#login_window').css('top') == '0px') {

 				        $('#login_window').delay(800).animate({
 				            top: '-=145px'
 				        }, 700, function () {
                            //EDIT: Martin Opršal - Allium 28.1.2011
 				            $('#PasswordRetrievalFormDiv').css('display', 'none');
 				            $('#LoginFormDiv').css('display', 'block');
                            //END EDIT
 				            $('#login_window').css('top', '-145px');
 				            $('#login_first_a').html('<strong>Přihlásit se </strong>');
 				        });

 				    }
 				}
 			);

        $('#login_window')
 			.bind(
 				'mouseenter',                    // on mouseleave
 				function () {
 				    if (parseInt($('#login_window').css('top')) > -135) {

 				        $('#login_window').stop().animate({
 				            top: '0px'
 				        }, 500, function () {
 				            $('#login_window').css('top', '0px');
 				        });
 				        $('#login_first_a').html('Zapomenuté heslo');

 				    }
 				}
 			);
        /********************************************************************/
        /*     script name: Show box                                        */
        /*     describe: find selected box and roll div on it               */
        /********************************************************************/
        //$('#icons_bar a') - EDIT: Martin Opršal (Allium) 21.1.2011
        $('#main_box #icons_bar a')
 			.bind(
 				'click',                    // on click
 				function (e) {
 				    e.preventDefault();

 				    var string = this.toString();
 				    var box = string.substring(string.lastIndexOf('#') + 1, string.length);
 				    var position = (box - 1) * 241 + parseInt($('#inner_info_boxes').css('left'));
 				    active_box = (box - 1);

 				    $('#icons_bar a.active').removeClass('active');
 				    $(this).addClass('active');

 				    position = '-=' + position.toString() + 'px';
 				    $('#inner_info_boxes').animate({                                                        // switch page one on page two
 				        left: position
 				    }, 700, function () {

 				        if (parseInt($('#inner_info_boxes').css('left')) < -1900) {
 				            $('#inner_info_boxes').animate({                                                        // switch page one on page two
 				                left: '-1928px'
 				            }, 800, function () {
 				                if (parseInt($('#inner_info_boxes').css('left')) < -1900) {
 				                    $('#inner_info_boxes').css('left', '-1928px');
 				                }
 				            });
 				        }
 				    });
 				}
 			);
        /********************************************************************/
        /*     script name: Move to next box                                */
        /*     describe: move one box to right                              */
        /********************************************************************/
        $('#mb_right a')
			.bind(
				'click',                    // on click
				function (e) {

				    e.preventDefault();

				    if (active_box < 11) {
				        active_box++;
				        $('#icons_bar a.active').removeClass('active');
				        $('#icons_bar a:eq(' + active_box + ')').addClass('active');
				    }

				    if (parseInt($('#inner_info_boxes').css('left')) > -1900) {

				        $('#inner_info_boxes').animate({
				            left: '-=241px'
				        }, 700, function () {



				            if (parseInt($('#inner_info_boxes').css('left')) < -1900) {
				                $('#inner_info_boxes').animate({
				                    left: '-1928px'
				                }, 500, function () {

				                    if (active_box >= 12) {
				                        active_box = 11;
				                        $('#icons_bar a.active').removeClass('active');
				                        $('#icons_bar a:eq(' + active_box + ')').addClass('active');
				                    }
				                    $('#inner_info_boxes').css('left', '-1928px');
				                });
				            }
				        });


				    }
				}
			);

        /********************************************************************/
        /*     script name: Move to next box                                */
        /*     describe: move one box to left                               */
        /********************************************************************/
        $('#mb_left a')
			.bind(
				'click',                    // on click
				function (e) {

				    e.preventDefault();

				    if (parseInt($('#inner_info_boxes').css('left')) < 0) {
				        $('#inner_info_boxes').animate({                                                        // switch page one on page two
				            left: '+=241px'
				        }, 700, function () {
				            if (active_box > 0) {
				                if (active_box > 7) {
				                    active_box = 7;
				                }
				                else
				                    active_box--;
				                $('#icons_bar a.active').removeClass('active');
				                $('#icons_bar a:eq(' + active_box + ')').addClass('active');
				            }

				            if (parseInt($('#inner_info_boxes').css('left')) > 0)
				                $('#inner_info_boxes').animate({
				                    left: '0px'
				                }, 500, function () {

				                    $('#inner_info_boxes').css('left', '0px');
				                });
				        });


				    }
				}
			);

        /********************************************************************/
        /*     script name: Actuallity box control                          */
        /*     describe: Fade out old and fade in selected actuallity       */
        /********************************************************************/

        $('#actuallity_pager a:eq(0)')
			.bind(
				'click',                    // on click
				function (e) {
				    e.preventDefault();
				    clearTimeout(news_changer);
				    news_changer = setTimeout(showNews, 30000);

				    if ($('#news2').css('display') == 'block') {

				        $('#actuallity_pager a:eq(1)').removeClass('active');
				        $('#actuallity_pager a:eq(0)').addClass('active');
				        $('#news2').fadeOut(700, function () { $('#news2').css('display', 'none'); $('#news1').fadeIn(700, function () { $('#news1').css('display', 'block'); }); });
				    }
				    else if ($('#news3').css('display') == 'block') {

				        $('#actuallity_pager a:eq(2)').removeClass('active');
				        $('#actuallity_pager a:eq(0)').addClass('active');
				        $('#news3').fadeOut(700, function () { $('#news3').css('display', 'none'); $('#news1').fadeIn(700, function () { $('#news1').css('display', 'block'); }); });
				    }
				    else {
				        $('#news1').css('display', 'block');
				        $('#news2').css('display', 'none');
				        $('#news3').css('display', 'none');
				    }

				}
			);

        $('#actuallity_pager a:eq(1)')
			.bind(
				'click',                    // on click
				function (e) {
				    e.preventDefault();
				    clearTimeout(news_changer);
				    news_changer = setTimeout(showNews, 30000);

				    if ($('#news1').css('display') == 'block') {

				        $('#actuallity_pager a:eq(0)').removeClass('active');
				        $('#actuallity_pager a:eq(1)').addClass('active');
				        //                 $('#news1').animate({                                                        // switch page one on page two
				        //                   opacity: '0'
				        //                   }, 700, function() {
				        //                          $('#news1').css('display','none');
				        //                          $('#news2').css('opacity','0');
				        //                          $('#news2').css('display','block');
				        //                          $('#news2').animate({                                                        // switch page one on page two
				        //                             opacity: '1'
				        //                             }, 700, function() {
				        //                                    //$('#news2').css('display','block');                            
				        //                           });                 
				        //                   });
				        $('#news1').fadeOut(700, function () { $('#news1').css('display', 'none'); $('#news2').fadeIn(700, function () { $('#news2').css('display', 'block'); }); });
				    }
				    else if ($('#news3').css('display') == 'block') {

				        $('#actuallity_pager a:eq(2)').removeClass('active');
				        $('#actuallity_pager a:eq(1)').addClass('active');
				        $('#news3').fadeOut(700, function () { $('#news3').css('display', 'none'); $('#news2').fadeIn(700, function () { $('#news2').css('display', 'block'); }); });
				    }
				    else {
				        $('#news2').css('display', 'block');
				        $('#news1').css('display', 'none');
				        $('#news3').css('display', 'none');
				    }

				}
			);

        $('#actuallity_pager a:eq(2)')
			.bind(
				'click',                    // on click
				function (e) {

				    e.preventDefault();
				    clearTimeout(news_changer);
				    news_changer = setTimeout(showNews, 30000);

				    if ($('#news1').css('display') == 'block') {

				        $('#actuallity_pager a:eq(0)').removeClass('active');
				        $('#actuallity_pager a:eq(2)').addClass('active');
				        $('#news1').fadeOut(700, function () { $('#news1').css('display', 'none'); $('#news3').fadeIn(700, function () { $('#news3').css('display', 'block'); }); });
				    }
				    else if ($('#news2').css('display') == 'block') {

				        $('#actuallity_pager a:eq(1)').removeClass('active');
				        $('#actuallity_pager a:eq(2)').addClass('active');
				        $('#news2').fadeOut(700, function () { $('#news2').css('display', 'none'); $('#news3').fadeIn(700, function () { $('#news3').css('display', 'block'); }); });
				    }
				    else {
				        $('#news3').css('display', 'block');
				        $('#news2').css('display', 'none');
				        $('#news1').css('display', 'none');
				    }

				}
			);

        /********************************************************************/
        /*     script name: Main menu                                       */
        /*     describe: Two level menu in header control                   */
        /********************************************************************/
        $('#main_menu ul li')
			.bind(
				'mouseenter',                    // on mouseenter
				function () {

				    if ($('#main_menu ul li .lastActive').length == 0)
				        $('#main_menu ul li .active').addClass('lastActive');
				    $('#main_menu ul li .active').removeClass('active');
				    $(this).find('a').addClass('active');
				    $('#main_submenu ul').css('display', 'none');
				    var submenu = $('#main_menu ul li').index(this);
				    $('#main_submenu ul:eq(' + submenu + ')').css('display', 'block');
				}
			);


        $('#header_bottom')
			.bind(
				'mouseleave',                    // on mouseleave
				function () {
				    $('#main_menu ul li .active').removeClass('active');
				    $('#main_menu ul li .lastActive').addClass('active');
				    $('#main_menu ul li .lastActive').removeClass('lastActive');
				    var submenu = $('#main_menu ul li a').index($('#main_menu ul li a.active'));
				    $('#main_submenu ul').css('display', 'none');
				    $('#main_submenu ul:eq(' + submenu + ')').css('display', 'block');
				}
			);

        $('#header_bottom')
			.bind(
				'mouseenter',                    // on mouseenter
				function () {
				    if ($('#main_menu ul li .lastActive').length == 0)
				        $('#main_menu ul li .active').addClass('lastActive');
				}
			);

        /********************************************************************/
        /*     script name: Login inputs                                    */
        /*     describe: change login input appearance                      */
        /********************************************************************/
        $('#login_window_content input')
			.bind(
				'focus',                    // on focus
				function () {
				    var input = $('#login_window_content input').index(this);
				    $('#login_window_content input:eq(' + input + ')').addClass('focus');
				}
			);

        $('#login_window_content input')
			.bind(
				'blur',                    // on mouseleave
				function () {
				    var input = $('#login_window_content input').index(this);
				    $('#login_window_content input:eq(' + input + ')').removeClass('focus');
				}
			);

        /********************************************************************/
        /*     script name: Anketa                                          */
        /*     describe: just for fun, can be deleted                       */
        /********************************************************************/
        $('#anketa a')
    .bind(
				'click',                    // on click
				function (e) {
				    e.preventDefault();
				    var vote = ($('#anketa a').index(this)) + 1;

				    $('#result' + vote + '').html(parseInt($('#result' + vote + '').html()) + 1);
				    var all = parseInt($('#result1').html()) + parseInt($('#result2').html()) + parseInt($('#result3').html());
				    $('#result_bar1').css('width', (160 * (parseInt($('#result1').html()) / all)) + 'px');
				    $('#result_bar2').css('width', (160 * (parseInt($('#result2').html()) / all)) + 'px');
				    $('#result_bar3').css('width', (160 * (parseInt($('#result3').html()) / all)) + 'px');
				    //alert("Děkujeme za Váš názor");
				}
			);
        /********************************************************************/
        /*     end of JQUERY.document.ready                                 */
        /********************************************************************/
    }
);

function showNews() {       // automatic update news box

    var news;
    if ($('#news1').css('display') == 'block') news = 1;
    else if ($('#news2').css('display') == 'block') news = 2;
    else if ($('#news3').css('display') == 'block') news = 3;

    var next_news;
    if (news == 3) next_news = 1; else next_news = news + 1;


    $('#news' + news + '').fadeOut(700, function () {
        $('#news' + news + '').css('display', 'none');
        $('#actuallity_pager a:eq(' + (news - 1) + ')').removeClass('active');
        $('#actuallity_pager a:eq(' + (next_news - 1) + ')').addClass('active');
        $('#news' + next_news + '').fadeIn(700, function () { $('#news' + next_news + '').css('display', 'block'); });
    });


    news_changer = setTimeout(showNews, 10000);
}
