//menu
jQuery( document ).ready( function() {
	if ( jQuery.browser.opera ) {
		jQuery( '#topNavigation li li li a' ).addClass( 'opera' );
	}
} );

jQuery( '#topNavigation h4 a' ).live( 'click', function() {
	if ( navigator.userAgent.match(/android/i) != null ||
		 navigator.userAgent.match(/iPhone/i) != null ||
         navigator.userAgent.match(/iPad/i) != null
	   ) {
		if ( jQuery( this ).parent( 'h4' ).next( 'ul' ).length == 1 ) {
			return false;
		}
	}

} );

jQuery( '#topNavigation > li:not(.hover)' ).live( 'mouseover', function() {
	jQuery( this ).children( 'ul' ).parent().addClass( 'hover' );
} );

jQuery( '#topNavigation > li.hover' ).live( 'mouseout', function() {
	jQuery( this ).removeClass( 'hover' );
} );



jQuery( '.stdTable2.switchCols td[data-col]:not(.checked), .stdTable2.switchCols th[data-col]:not(.checked)' ).live( 'click', function() {
	var child = jQuery( this ).attr( 'data-col' );
	var table = '#' + jQuery( this ).parents( 'table.stdTable2.switchCols' ).attr( 'id' );
	jQuery( table + ' *' ).removeClass( 'checked' );
	jQuery( table + ' tr > :nth-child(' + child + ')' ).addClass( 'checked' );
	jQuery( table + ' .order' ).removeClass( 'orderActive' );
	jQuery( table + ' tr > :nth-child(' + child + ') .order' ).addClass( 'orderActive' );
	//jQuery( 'a[data="' + jQuery(table).attr('id') + '"]' ).show();
	if ( jQuery( table + ' tr > :nth-child(' + child + ') :radio:checked' ).length == 0 ) {
		jQuery( table + ' tr > :nth-child(' + child + ') :radio:first' ).attr( 'checked', 'checked' );
	}
} );





jQuery( document ).ready( function() {

	jQuery('.addServices tr').removeClass('odd even');
	jQuery('.addServices tr:nth-child(even)').addClass('even');
	jQuery('.addServices tr:nth-child(odd)').addClass('odd');

	jQuery('.stdTable2 tr:nth-child(even)').addClass('even');
	jQuery('.stdTable2 tr:nth-child(odd)').addClass('odd');

	jQuery('.marketplaceList tr:nth-child(even)').addClass('even');
	jQuery('.stdTable2 .inputText, .stdTable2 select').bind('blur focus', function(){
		jQuery(this).toggleClass('formElementActive');
	});

	jQuery('.wscTable tr:nth-child(2n+1)').not(':nth-child(1)').addClass('even');

	jQuery('#mainRow1 #seltld').selectbox();
	jQuery('#seltld_input_inne').click(function(){
		var objOverlay = new Overlay( "topSearchDomainForm" );
		objOverlay.show();
		window.location = '/domeny-zagraniczne.html';
	});

	jQuery('#mainRow1 .tld').append('<span class="arrow"></span>');
	var homePricingInputText = jQuery('#mainRow2 .pricing .inputText').val();
	jQuery('#mainRow2 .inputText').bind("click focus", function(){
		if(jQuery(this).val()==homePricingInputText){
			jQuery(this).val('');
		}
	});
	jQuery('#mainRow2 .inputText').blur(function(){
		if(jQuery(this).val()==''){
			jQuery(this).val(homePricingInputText);
		}
	});
	jQuery( 'form[action="/wyniki-wyszukiwania.html"] input[type="submit"], form[action="/domeny-internetowe.html"] input[type="submit"]' ).click( function() {
		var error = false;
        var objForm = jQuery( this ).parents( 'form' );
		objForm.find( '#domain, #domainname' ).each( function() {
			var domain = jQuery( this ).val();
			if ( domain == 'Wpisz nazwę domeny...' || domain == 'Wpisz nazwę i sprawdź, czy jest wolna...' || domain == 'www.' || domain == '' || domain == 'wpisz nazwę domeny' ) {
				alert( 'Wpisz nazwę domeny.' );
				error = true;
			} // endif
		} );
        if ( objForm.hasClass( 'noAutoLoading' ) ) {
            return error ? false : true;
        }
		return error ? false : loading( this.form );
	} );

	jQuery('.btnInput').hover(function(){
		jQuery(this).toggleClass('btnInputActive');
		jQuery(this).children('.right').toggleClass('rightActive');
	});

	jQuery('.btnLarge').hover(function(){
		jQuery(this).toggleClass('btnLargeActive');
	});

	jQuery('.btnLargeGray').hover(function(){
		jQuery(this).toggleClass('btnLargeGrayActive');
	});

	jQuery('.ppContainer img').hover(function(){
		jQuery(this)
            .tooltipClass('overDivPP')
            .tooltipPP( '<span class="bold">' + jQuery(this).attr('alt') + '</span><br /><div style="padding-top: 6px; color: #0097d8" class="bold"><span style="font-size: 17px">' + jQuery(this).parent().attr('rel') + '</span> pkt.</div>' );
	});

	jQuery('.ppContainer').hover(
		function(){
			jQuery('.ppContainer > a').css('display','block');
		},
		function(){
			jQuery('.ppContainer > a').hide();
		}
	);

	var idPP = 1;
	var ppNavEnabled = true;

	jQuery('.ppContainer img').hover(
		function(){
			ppNavEnabled = false;
		},
		function(){
			ppNavEnabled = true;
		}
	);

	var changePP = setInterval(function() {
		if(ppNavEnabled){
			idPP = jQuery('#pp_' + parseInt(idPP+1)).length==1 ? idPP+1 : 1;
			jQuery('.ppContainer .pp').hide();
			jQuery('#pp_' + idPP).show();
		}
	}, 10000 );
	jQuery('.ppContainer .prev').click(function(){
		if(ppNavEnabled){
			ppNavEnabled = false;
			if(jQuery('#pp_1').is(':visible')){
				jQuery('.ppContainer .pp:visible').animate({width: 'hide'}, function(){
					jQuery('.ppContainer .pp:last').show();
					ppNavEnabled = true;
				});
				idPP = jQuery('.ppContainer .pp:last').attr('id').substr(3);
			}
			else {
				idPP--;
				jQuery('.ppContainer .pp:visible').animate({width: 'hide'}, function(){
					jQuery('#pp_' + idPP).show();
					ppNavEnabled = true;
				});
			}
		}
		clearInterval( changePP );
		return false;
	});
	jQuery('.ppContainer .next').click(function(){
		if(ppNavEnabled){
			ppNavEnabled = false;
			if(jQuery('.ppContainer .pp:last').is(':visible')){
				jQuery('.ppContainer .pp').hide();
				jQuery('.ppContainer .pp:first').animate({width: 'show'}, function(){
					ppNavEnabled = true;
				});
				idPP = 1;
			}
			else {
				jQuery('.ppContainer .pp').hide();
				jQuery('#pp_' + parseInt(idPP+1)).animate({width: 'show'}, function(){
					ppNavEnabled = true;
				});
				idPP++;
			}
		}
		clearInterval( changePP );
		return false;
	});

	//homepage btnTransfer tooltip
	jQuery('#mainRow1 .domains .transfer')
        .tooltipClass('overDivPP overDivTransfer')
        .tooltip('<b>Przenieś do nas domeny</b><br />dostaniesz świetne ceny<br />i 50% więcej punktów<br />w Programie Partnerskim');

	jQuery('#btnSearchDomainOption')
        .tooltipClass('overDivPP overDivTransfer')
        .tooltip('<b>Rezerwuj zajętą domenę&nbsp;&ndash;</b><br />będzie Twoja, gdy się zwolni');

	jQuery('#btnSearchDomainDnt')
        .tooltipClass('overDivPP overDivTransfer')
        .tooltip('Tylko u nas<br /><b>Testowanie Domen</b> od 0 zł');

});



jQuery('.cssButtonOrange').live( 'hover', function() {
	jQuery( this ).toggleClass( 'cssButtonOrangeHover' );
} );


var globalAjax = new Array();


var domainChecker2 = function()
{
    var that = {},
        domainsListToCheck = [new Array(), new Array()],
        queueCounter = domainsListToCheck.length,
        loading;

    // dodaje domene do listy
    that.addDomain = function( intGroup, strDomainName, strId, boolIsChecked )
    {
        domainsListToCheck[ intGroup ].push( {name: strDomainName, id: strId, boolChecked: boolIsChecked} );
    };


    // zwraca domeny
    that.getDomains = function( intSetGroup )
    {
        if ( 'undefined' === typeof domainsListToCheck[ intSetGroup ] ) {
            throw 'Nie znaleziono grupy "'+intSetGroup+'"';
        }

        return domainsListToCheck[ intSetGroup ];
    }; // getDomains()


    that.checkDomains = function( intSetGroup )
    {
        if ( 'undefined' === typeof loading ) {
            jQuery( '.loadingText' ).show();
            loading = setInterval( function() {
                if ( jQuery( '.loadingText span' ).html() == '...' ) {
                    jQuery( '.loadingText span' ).html( '' );
                }
                else {
                    jQuery( '.loadingText span' ).append( '.' );
                }
            }, 100 );
        } // endif

        var strData = 'command=check';
        var arrDomains = that.getDomains( intSetGroup ), intI;
        for ( intI=0; intI<arrDomains.length; intI++ ) {
            strData += '&name[]='+arrDomains[intI].name;
        } // endfor

        if ( arrDomains.length == 0 ) {
            complete(intSetGroup);
            return ;
        }

        var obj = jQuery.ajax({
            url: '/szukaj-domeny-internetowe.html',
            dataType: 'json',
            data: strData,
            async: true,
            beforeSend: function() {
            },
            success: function( objResult ) {
                try {
                    if ( 'undefined' === typeof objResult ) {throw 'Brak obiektu rezultatu';}
                    if ( 'undefined' === typeof objResult.domains ) {throw 'Brak obiektu domains';}
                    parseResult( intSetGroup, objResult );
                }
                catch( e ) {
                    jQuery( that.getDomains( intSetGroup ) ).each(function(i,v){
                        makeResultForDomain( v.id, -1, false );
                    });
                }

                complete(intSetGroup);
            },
            error: function() {
                jQuery( that.getDomains( intSetGroup ) ).each(function(i,v){
                    makeResultForDomain( v.id, -1, false );
                });

                complete(intSetGroup);
            } // error
        });

        globalAjax.push( obj );
    }; // checkDomains()


    var parseResult = function( intSetGroup, objResult )
    {
        var arrDomains = that.getDomains( intSetGroup );

        for ( intI=0; intI<arrDomains.length; intI++ ) {
            if ( 'undefined' === typeof arrDomains[ intI ] ) {continue;}
            if ( 'undefined' === typeof arrDomains[ intI ].name ) {continue;}

            var strDomainName = arrDomains[ intI ].name;
            if ( 'undefined' === typeof objResult.domains[ strDomainName ] ) {
                throw 'Brak rezultatu dla domeny "'+strDomainName+'"';
            }

            var intStatus = parseInt( objResult.domains[ strDomainName ], 10 );
            makeResultForDomain( arrDomains[ intI ].id, intStatus, arrDomains[ intI ].boolChecked );
        } // endfor
    }; // parseResult()


    var makeResultForDomain = function( strId, intStatus, boolChecked )
    {
        var objInput  = jQuery('#'+strId);
        var objTr = objInput.closest('tr');

        if ( intStatus == 1 ) { // wolna
            //objInput.attr('checked', boolChecked);
            objTr.find( 'td.status' ).html( '<span class="icoTrue">dostępna</span>' );
            objTr.find( 'td.checkbox input' ).removeAttr( 'disabled' );
        } else { // zajeta
            objTr.remove();

            jQuery('.futuresResult').show();
            var strDomainName = objInput.attr('value');

            objTr = jQuery( '.futuresResult tr.tocheck[data="'+strDomainName+'"]' );

            if ( intStatus < 0 ) { // jakis blad
                jQuery( 'td.status ', objTr ).html( '<span class="icoWarning">brak informacji</span>' )
            }
        } // endif

        objTr.fadeIn();
    }; // makeResultForDomain


    var complete = function(intGroup)
    {
        queueCounter--;
        if ( 0 == queueCounter ) {
            jQuery('.searchResults .icoWarning').tooltip( 'Brak informacji o dostępności domeny' );

            jQuery( '.loadingText' ).hide();
            jQuery( '.domenytext' ).show();
			var a = jQuery( '.domainList.searchResults.freeDomains tbody' );
            a.find( 'tr:nth-child(1) :checkbox' ).attr( 'checked', 'checked' );
            a.find( 'tr:nth-child(2) :checkbox' ).attr( 'checked', 'checked' );
            //jQuery( 'td.loadingText' ).parent().hide();
            clearInterval( loading );

            // sprawdzenie czy sa jakies wolne domeny, jak nie to chowamy cala tabelke z wolnymi
            if ( 0 == jQuery('.domainList.searchResults.freeDomains tbody tr:visible').length ) {
                jQuery('.domainList.searchResults.freeDomains').hide();
                jQuery('#searchdomainresultform h1.large').hide();
                jQuery('p.promoinfo').hide();
                jQuery('div.battontohide').hide();
            } // endif
        } // endif
    }; // complete()

    return that;
}; // domainChecker2
