// main.js

var galleryPictureStep = 5;
var picturePointer = 0;

// jobs.cz
var jobRotateTime = 11000;

// hdworld
var hdwTitlePointer = 0;
var openList = new Array();
//var autoSwitchTime = 11000;
var autoSwitchTime = 5000;
var autoSwitchStack;

$('document').ready(function() {
    // onclick pro prihlasovaci formular
    $('#name,#passwd').click(function() {
        if ( $('#name').attr("value") == $('#name').attr("title") )
            $('#name').attr("value", "");
        if ( $('#passwd').attr("value") == $('#passwd').attr("title") )
            $('#passwd').attr("value", "");
    });
    
    // rotace jobs.cz boxu
    setTimeout("jobRotate()", jobRotateTime);
        
    // autoswitch HDworldu
    if (openList.length > 0)
    {
        $("ul.image_nav_new").everyTime(autoSwitchTime, 'image-switch', function() {
    		autoSwitchTitle();
    	});
        //autoSwitchStack = setTimeout("autoSwitchTitle()", autoSwitchTime);
    }
    
    // preklikavatko HDworldu
    $("ul.image_nav > li > a, ul.image_nav_new > li > a").click(function(){
        if ( this.rel )
        {
            hdwTitlePointer = this.rel - 1;
        }
        else
        {
            if ( this.parentNode.className == "next" && hdwTitlePointer > 0 )
            {
                hdwTitlePointer--;
            }
            else if ( this.parentNode.className == "back" && hdwTitlePointer < 2 )
            {
                hdwTitlePointer++;
            }
        }
        
        // switch image
        switchTitleImage(hdwTitlePointer);
        
        // reset timeru
        //$(this).stopTime('image-switch');
        $("ul.image_nav_new").stopTime();
        
        // novy timer
        $("ul.image_nav_new").everyTime(autoSwitchTime, 'image-switch', function() {
    		autoSwitchTitle();
    	});
    });
    
    // preklikavatko tabu
    $("ul.boxtabs > li > a").click(function(){
        $.each( $("ul.boxtabs > li > a"), function(){
            $("#"+this.rel).css({'display' : 'none'});
        });
        $.each( $("ul.boxtabs > li"), function(){
            this.className = "";
        });
        
        $("#"+this.rel).css({'display' : ''});
        
        this.parentNode.className = "active";
        
        return false;
    });
    
    // posun v galerii
    $("ul.image_roll > li.roll_back > a, ul.image_roll > li.roll_next > a").click(function(){
        var pictureCount = 0;
        var pictureOffset = 0;
        
        $.each( $("ul.image_roll > li"), function(){
            if ( this.className != "roll_back" && this.className != "roll_next" )
                pictureCount++;
        });
        
        if (this.parentNode.className == "roll_back")
        {   
            if (picturePointer - galleryPictureStep > 0)
            {
                picturePointer = picturePointer - galleryPictureStep;
            }
            else
            {
                picturePointer = 0;
            }
        }
        else
        {
            if (pictureCount > picturePointer + galleryPictureStep)
            {
                picturePointer = picturePointer + galleryPictureStep;
            }
        }
        
        picturePointerEnd = picturePointer + galleryPictureStep;
        
        $.each( $("ul.image_roll > li"), function(){
            if ( this.className != "roll_back" && this.className != "roll_next" )
            {
                if (pictureOffset >= picturePointer && picturePointerEnd > pictureOffset)
                {
                    this.style.display = "";
                }
                else
                {
                    this.style.display = "none";
                }
            
                pictureOffset++;
            }
        });
    });
    
    // preklikavatko gamestar
    $("ul.game_type > li > a").click(function()
    {    
        $.get("/ajax/load-game-list", { category: this.rel },
            function(data){
                document.getElementById('game_deck').innerHTML = data;
            }
        );
        
        $.each( $("ul.game_type > li"), function(){
            this.className = "";
        });
        this.parentNode.className = "active";
        
        return false;
    });


    // newsletter box
    makeNewsletterBoxAjax();

});

function makeNewsletterBoxAjax()
{
    $('.newsletter_box_target #newsletter_form').ajaxForm({
        success: function(data) {
            $('.newsletter_box_target').html(data);
            makeNewsletterBoxAjax();
        }
    });
}

function switchTitleImage(hdwTitlePointer)
{
    // nastavime promenne
    $("#open_perex").text( openList[ parseInt(hdwTitlePointer) ]['perex'] );
    $("#open_title").text( openList[ parseInt(hdwTitlePointer) ]['title'] );
    $("#open_title").attr("href", openList[ parseInt(hdwTitlePointer) ]['url']);
    $("#open_image_link").attr("href", openList[ parseInt(hdwTitlePointer) ]['url']);
    $("#open_button_link").attr("href", openList[ parseInt(hdwTitlePointer) ]['url']);
    
    $("#open_image").fadeOut(400, function () {
        $("#open_image").attr("src", openList[ parseInt(hdwTitlePointer) ]['image']);
        $("#open_image").fadeIn(800);
    });
    
    // sipecky
    var pictureOffset = 0;
    $.each( $("ul.image_nav > li"), function(){
        if ( this.className != "back" && this.className != "next" )
        {
            if (pictureOffset == hdwTitlePointer)
                this.className = "active";
            else
                this.className = "";
            
            pictureOffset++;
        }
    });
    
    // sipecky NEW
    var pictureOffset = 0;
    $.each( $("ul.image_nav_new > li"), function(){
        if ( this.className != "back" && this.className != "next" )
        {
            if (pictureOffset == hdwTitlePointer)
                this.className = "active";
            else
                this.className = "inactive";
            
            pictureOffset++;
        }
    });
    
    //clearTimeout(autoSwitchStack);
    //autoSwitchStack = setTimeout("autoSwitchTitle()", autoSwitchTime);
}

function autoSwitchTitle()
{
    hdwTitlePointer++;
    if (hdwTitlePointer > 2) hdwTitlePointer=0;

    //alert( "pointer:"+hdwTitlePointer );
    switchTitleImage(hdwTitlePointer);

    //clearTimeout(autoSwitchStack);
    //autoSwitchStack = setTimeout("autoSwitchTitle()", autoSwitchTime);
}


function jobRotate()
{
    var firstJobItem = $("#jobsczbox li:last").html();
    $("#jobsczbox li:last").remove();
    $("#jobsczbox").prepend("<li>"+firstJobItem+"</li>");

    setTimeout("jobRotate()", jobRotateTime);
}

function copyFormGroup(id)
{
    var origHtml = $("#form_group_"+id).html();
    var newIdx = $("#form_groupcont_"+id+" > table").length;
    var newHtml =
        '<table class="form" cellspacing="0">' + 
        origHtml.replace(
			     /name="([a-z0-9]+)\[0\](\[[a-z0-9]+\])"/g,
			     'name="$1['+newIdx+']$2"'
	      ).replace(
			     /name=([a-z0-9]+)\[0\](\[[a-z0-9]+\])/g,
			     'name="$1['+newIdx+']$2"'
	      ).replace(
			     /(<input[^>]*)value=[^\s]*/g,
			     '$1value=""'
	      )
        +
        '</table>';
    $("#form_groupcont_"+id).append( newHtml );
    
    return false;
}

function deleteFormGroup(elt)
{
    $(elt).parent().parent().parent().parent().remove();
    
    return false;
}

function toggleDisplay(id)
{
    if ($("#"+id+"_data").css('display') == 'none')
    {
        $("#"+id+"_label").text($("#"+id+"_label").text().replace(/\(\+\)/g, "(-)"));
        $("#"+id+"_data").show();
    }
    else
    {
        $("#"+id+"_label").text($("#"+id+"_label").text().replace(/\(\-\)/g, "(+)"));
        $("#"+id+"_data").hide();
    }
    
    return false;
}





