$(function() {
        
    ////Show the paging and activate its first link
    $(".paging").show();
    $(".paging a:first").addClass("active");

    //Get size of the image, how many images there are, then determin the size of the image reel.
    var imageWidth = $(".window").width();
    var imageSum = $(".image_reel img").size();
    var imageReelWidth = imageWidth * imageSum;

    //Adjust the image reel to its new size
    $(".image_reel").css({
        'width' : imageReelWidth
    });

    //Paging  and Slider Function
    rotate = function(){
        var triggerID = $active.attr("rel") - 1; //Get number of times to slide
        var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

        $(".paging a").removeClass('active'); //Remove all active class
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

        //Slider Animation
        $(".image_reel").animate({
            left: -image_reelPosition
        }, 500 );

    };

    //Rotation  and Timing Event
    rotateSwitch = function(){
        play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
            $active = $('.paging a.active').next(); //Move to the next paging
            if ( $active.length === 0) { //If paging reaches the end...
                $active = $('.paging a:first'); //go back to first
            }
            rotate(); //Trigger the paging and slider function
        }, 7000); //Timer speed in milliseconds (7 seconds)
    };

    rotateSwitch(); //Run function on launch

    //On Hover
    $(".image_reel a").hover(function() {
        clearInterval(play); //Stop the rotation
    }, function() {
        rotateSwitch(); //Resume rotation timer
    });

    //On Click
    $(".paging a").click(function() {
        $active = $(this); //Activate the clicked paging
        //Reset Timer
        clearInterval(play); //Stop the rotation
        rotate(); //Trigger rotation immediately
        rotateSwitch(); // Resume rotation timer
        return false; //Prevent browser jump to link anchor
    });

    //
    //scroll
    //
    //
    //create scroller for each element with "horizontal_scroller" class...
    $('#news-bar').SetScroller({
        velocity: 	 60,
        direction: 	 'horizontal',
        startfrom: 	 'right',
        loop:		 'infinite',
        movetype: 	 'linear',
        onmouseover:     'pause',
        onmouseout:       'play',
        onstartup: 	 'play',
        cursor: 	 'pointer'
    });
    /*
		All possible values for options...

		velocity: from 1 to 99	[default is 50]
		direction: 		'horizontal' or 'vertical' 					[default is 'horizontal']
		startfrom: 		'left' or 'right' for horizontal direction 	[default is 'right']
						'top' or 'bottom' for vertical direction	[default is 'bottom']
		loop:			from 1 to n+, or set 'infinite'				[default is 'infinite']
		movetype:		'linear' or 'pingpong'						[default is 'linear']
		onmouseover:	'play' or 'pause'							[default is 'pause']
		onmouseout:		'play' or 'pause'							[default is 'play']
		onstartup: 		'play' or 'pause'							[default is 'play']
		cursor: 		'pointer' or any other CSS style			[default is 'pointer']
     */




    $('.subnav').each(function () {
        $(this).parent().eq(0).hover(function () {
            $('.subnav:eq(0)', this).show(150);
        }, function () {
            $('.subnav:eq(0)', this).hide(300);
        });
    });

    $('#other-prices-link-active').click(function () {
        $('#other-prices-table').hide(200);
        $(this).hide();
        $('#other-prices-link').css('display', 'inline-block');
        $('#footer').show();
    });

    $('#other-prices-link').click(function () {
        $('#other-prices-table').show(200);
        $(this).hide();
        $('#other-prices-link-active').css('display', 'inline-block');
        $('#footer').hide();
    });

    $.ajaxSetup({
        type: 'POST',
        async: false,
        dataType: 'json'
    });

    //formularz kontaktowy

    $( "#dialog:ui-dialog" ).dialog( "destroy" );

    var number = $( "#number" ),
    phone = $( "#phone" ),
    data = $( '#data' ),
    info = $( '#info' ),
    allFields = $( [] ).add( number ).add( phone ),
    tips = $( ".validateTips" ),
    wsCount = 4;

    function updateTips( t ) {
        tips
        .text( t )
        .addClass( "ui-state-highlight" )
        .addClass("ui-state-error-text");
        setTimeout(function() {
            tips.removeClass( "ui-state-highlight", 1500 ).removeClass( "ui-state-error-text", 1500 );
        }, 500 );
    }

    function checkRegexp( o, regexp, n ) {
        if ( !( regexp.test( o.val() ) ) ) {
            o.addClass( "ui-state-error" );
            updateTips( n );
            return false;
        } else {
            return true;
        }
    }

    function checkRadio( n ) {
        if ( $( 'input[name="case"]:checked' ).val() == undefined ) {
            updateTips( n );
            return false;
        } else {
            return true;
        }
    }

    function checkBox( o, n ) {
        if ( o.is(':checked')) {
            return true;
        } else {
            o.addClass( "ui-state-error" );
            updateTips( n );
            return false;
        }
    }

    function successClose() { 
        if((wsCount - 1) >= 0){ 
            wsCount = wsCount - 1; 
            tips
            .addClass( "ui-state-highlight" )
            .html('Twoje zg\u0142osznie zostało przyjęte. To okno zamknie się automatycznie: <font style="font-size:110%;"> <b>' + wsCount + '</b></font>...'); 
 
            setTimeout(successClose, 1000);    
        } 
        else{ 
            $( "#dialog:ui-dialog" ).dialog( "destroy" );
            $( "#dialog-form" ).dialog( "close" );
        } 
 
    }

    $( "#dialog-form" ).dialog({
        autoOpen: false,
        draggable: false,
        resizable: false,
        height: 270,
        width: 700,
        modal: true,
        buttons: [
        {
            text: "Wyślij",
            id: "btn_send",
            click: function() { 
                var bValid = true;

                allFields.removeClass( "ui-state-error" );


                bValid = bValid && checkRegexp( number, /^([A-Z]{2})?([0-9])+$/i, "Niepoprawny numer dystrybutora." );
                bValid = bValid && checkRegexp( phone, /^([0-9])+$/, "Dopuszczalne znaki dla numeru telefonu to cyfry od 0 do 9." );
                bValid = bValid && checkRadio( "Proszę wybrać sprawę." );
                bValid = bValid && checkBox( data, "Zgoda na przetwarzanie danych osobowych jest wymagana." );

                if ( bValid ) {
                    affa = $( 'input[name="case"]:checked' ).val();
                    $.ajax({
                        url: 'index.php/contact/index',
                        data: 'number='+number.val()+'&phone='+phone.val()+'&case='+affa,
                        ajaxStart: $(":button").button( "option", "disabled", true ),
                        success: function(data)
                        {
                            if(data == true) {
                                $("#btn_close").button( "option", "disabled", false ),
                                successClose();
                            }
                        }
                    });
                }
            }
        },
        {
            text: "Zamknij",
            id: "btn_close",
            click: function() {
                $( this ).dialog( "close" );
            }
        }
        ],
        close: function() {
            allFields.val( "" ).removeClass( "ui-state-error" );
        }
    });

    $("#button_kontakt" )
    .css('cursor', 'pointer')
    .click(function() {
        $("#dialog-form" ).dialog( "open" );
    });

    $("#dialog-form-przetwarzanie-more")
    .css('cursor', 'pointer')
    .click(function() {
        if($(this).html() == 'rozwiń') {
            $("#dialog-form" ).css('height', '369px');
            $("#dialog-form-przetwarzanie-long" ).show();
            $("#dialog-form-przetwarzanie-more").html('zwiń');
        } else {
            $("#dialog-form" ).css('height', '139px');
            $("#dialog-form-przetwarzanie-long" ).hide();
            $("#dialog-form-przetwarzanie-more").html('rozwiń');
        }
    });

    $("#fu_przetwarzanie-more")
    .css('cursor', 'pointer')
    .click(function() {
        if($(this).html() == 'rozwiń') {
            $("#fu_przetwarzanie-long" ).show();
            $("#fu_przetwarzanie-more").html('zwiń');
        } else {
            $("#fu_przetwarzanie-long" ).hide();
            $("#fu_przetwarzanie-more").html('rozwiń');
        }
    });
});

function fromFlashCall(val) {
    if(val) {
        $.ajax({
            url: 'index.php/netfm/data',
            data: 'contract='+val,
            success: function(data)
            {
                $.each(data, function(key, val){
                    var item = '#'+key;
           
                    $(item).children('.prize').html(val);
                });
            }
        });

    }
}


