/*
 *taken from interspeedhosting.co.nz
 */
function galleria(){
    $('ul.gallery li img').each(function (i) {
        var desc = $(this).siblings('span').html();
        if( desc == null ) desc = "";
        $(this).attr('longdesc',desc);
    });
    
    $('ul.gallery').galleria({
        history   : false, // deactivates the history object for bookmarking, back-button etc.
        clickNext : false, // helper for making the image clickable. Let's not have that in this example.
        insert    : undefined, // the containing selector for our main image. 
                               // If not found or undefined (like here), galleria will create a container 
                               // before the ul with the class .galleria_container (see CSS)
        onImage   : function() { $('.nav').css('display','block'); } // shows the nav when the image is showing
    });
}

$(function(){
/*
     * Styling
     */
    
    /*
     * Pretty forms
     */
   // $('form.prettyform input[@type*=radio]').css('border','none');
   // $('form.prettyform input[@type*=checkbox]').css('border','none');
   //  if($.browser.mozilla) { prettyform(); }
    
    /*
     * LI hover fix for IE6
     */
    $('#navigation ul li').each( function(){
        $(this).mouseover( function(){ $(this).addClass('hover'); } ).mouseout( function(){ $(this).removeClass('hover'); } );
    });
    
    /*
     * Blur on focus
     */
    //$('a').focus(function() { this.blur(); });
    //$('input[@type*=submit]').focus(function() { this.blur(); });
    //$('input[@type=text], input[@type=password]').addClass("input");
    
    //$("h3 a ").append('<img class="arrow" border="0" src="/asset/images/red_arrow_right.gif" alt="" />');
    //$("h3.redBG a img").attr('src','/asset/images/doubleArrow_black.gif');
    //$("div.panel h2").append('<img class="arrow" border="0" src="/asset/images/red_arrow_right.gif" alt="" />');
    //$("#homepagePanels div.panel h2 img").attr('src',"/asset/images/red_arrow_right_darkbg.gif");
    //$("h3.get-in-touch a img").attr('src',"/asset/images/doubleArrow_black.gif");
    //$("h3.web-design a img ").attr('src',"/asset/images/red_arrow_right_darkbg.gif");
    
    $("input[name='domain'], input[name='domain'].small_input ").alphanumeric({allow:"-"});
    
    
    // Nav fix
    $("#mainNavigation_node99 a").attr('href','http://support.interspeed.co.nz/support/Agreement-and-Policies');
    
    
    // vertical slider
    
    
    // initialize gallery if need be
    /*
    if( $("a.lightbox").length > 0 ) {
        $("a.lightbox").lightBox({
            imageLoading: '/asset/images/lightbox/lightbox-ico-loading.gif',
            imageBtnClose: '/asset/images/lightbox/lightbox-btn-close.gif',
            imageBtnPrev: '/asset/images/lightbox/lightbox-btn-prev.gif',
            imageBtnNext: '/asset/images/lightbox/lightbox-btn-next.gif',
            fixedNavigation:false
        }); // Select all links with lightbox class
    }
    */
    if( document.URL.indexOf('?') > 0 ){
        $("#contact_details").hide();    
        $("#contact_details").before("<p><a class='toggleMore' href='javascript:void(0);'>Show Details</a></p>");
    }
        
    $(".toggleMore").toggle(function(){
        $(this).parent().next("div").slideDown();
        $(this).text("Hide Details");
        },function(){
        $(this).parent().next("div").slideUp();
        $(this).text("Show Details");
    });
    
    
    
    $("#registerForm").submit(function(){
    	/* $("#domainResults").empty().append('Begin your search...'); */
        var domain = $("#domainName").val();
        var domainStatus = false;
        var fullDomain = "";
        var extID = "";
        
        $("input:checkbox:checked").each( 
                function() { 
                   // Insert code here 
                    fullDomain = domain + $(this).val();
                    extID = $(this).val().replace(/\./g, "_");

                    domainStatus = checkDomain( fullDomain,extID );
                    $("#domainResults").append( 
                            "<div id='"+extID+"' class='loading'>" +
                                    "<strong>"+fullDomain+"</strong> Fetching Details...</div>");
                    //alert(fullDomain);
                    //alert(domainStatus);
                } 
            );
        return false;
    });
    
    if ($("#domainName").val() !== 'domainname') {
        /* $("#domainResults").empty().append('<p>No results to display.</p>'); */
        var domain = $("#domainName").val();
        var domainStatus = false;
        var fullDomain = "";
        var extID = "";
        
        $("input:checkbox:checked").each( 
                function() { 
                   // Insert code here 
                    fullDomain = domain + $(this).val();
                    extID = $(this).val().replace(/\./g, "_");

                    domainStatus = checkDomain( fullDomain,extID );
                    $("#domainResults").append( 
                            "<div id='"+extID+"' class='loading'>" +
                                    "<strong>"+fullDomain+"</strong> Fetching Details...</div>");
                    //alert(fullDomain);
                    //alert(domainStatus);
                } 
            );
        return false;
    }
    
    function checkDomain( fullDomain,extID ){

        $.ajax(
                {
                    url: '/services/whois_ws.php',
                    type: 'POST',
                    data: {domain:fullDomain},
                    dataType: 'xml',
                    async: true,
                    success: function(xml) {
                        var result =  $(xml).find('available').text();
                        var limit = $(xml).find('limit').text();
                        
                        $("#"+extID).removeClass("loading");
                        //
                        if(result == "true") {
                            $("#"+extID).addClass('available');
                            $("#"+extID).empty().append(fullDomain+" <a href='https://interspeed.co.nz/signup/domain?domain=" + escape(fullDomain) + "'>Available - Register Now</a> ");
                        } else {
                            if (limit == "true") {
                                $("#"+extID).addClass('unavailable');
                               $("#"+extID).empty().append(fullDomain+" Request Limit Reached");
                            } else {
                                $("#"+extID).addClass('unavailable');
                                $("#"+extID).empty().append(fullDomain+" Not Available");
                            }
                        }
                        //alert('Looking at '+fullDomain + " Result = " + result);
                        
                        return result;
                        // alert($(xml).find('domain').text());
                        // alert($(xml).find('available').text());       
                        //$('#someblock').append('')
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                            //alert(textStatus);
                            //alert(errorThrown);
                            $("#"+extID).removeClass("loading");
                            $("#"+extID).empty().append("Could not get status for <strong>"+fullDomain+"</strong>");
                        }
                }
            );
    }



});
    
    
    
function clearSearch(i){
    if( $(i).val() == "" ){
        $(i).val('Enter keyword...');
        $(i).addClass('inactive');
    }else if( $(i).val() == "Enter keyword..." ){
        $(i).val('');
        $(i).removeClass('inactive');
    }else{
        $(i).select();
    }
}

/*
 * Pretty forms
 */
function prettyform(){
  /*
   * Hide forms
   */
  $( 'form.prettyform' ).hide().end();
  /*
   * Label & list formatting
   */
  $( 'form.prettyform' ).find( 'label' ).not( '.noprettyform' ).each( function( i ){
    var labelContent = this.innerHTML;
    var labelWidth = document.defaultView.getComputedStyle( this, '' ).getPropertyValue( 'width' );
    var labelSpan = document.createElement( 'span' );
        labelSpan.style.display = 'block';
        labelSpan.style.width = labelWidth;
        labelSpan.innerHTML = labelContent;
    this.style.display = '-moz-inline-box';
    this.innerHTML = '';
    this.appendChild( labelSpan );
  } ).end();
  
  /*
   * Show forms
   */
  $( 'form.prettyform' ).show().end();
}

//Choose hosting account from colorbox
function selectHosting(){
		
		// Domain search box
		// $("#homeDomainSearch").submit();
		
		// Acutal submit form
		$("#hostingType").val( $("#hosting").val() );
		
//		$.fn.colorbox.close();
		
		$("#domainNameForm").submit();
};

