$(function(){
    /*
     * PAGE SETUP
     */

    //add actions to contact links
    $.localScroll();
    $.localScroll.hash();
    $('fieldset#comment textarea').resizable({
			handles: "se,s",
                        maxWidth: '530',
                        minWidth: '530'
		});


    // QTIP SETUP
    $('ul li ul li.featured-project img').qtip({
        style: { 
              width: 300,
              padding: 5,
              background: '#fff',
              color: '#333',
              textAlign: 'center',
              border: {
                 width: 0.4,
                 radius: 3,
                 color: '#959595'
              },
              tip: 'rightMiddle',
              name: 'light' 
        },

        position: {
            corner: {
                target: 'leftMiddle',
                tooltip: 'rightMiddle'
            }
        },
        show: {
            delay: 20,
            solo: true,
            effect: {
                type: 'fade',
                length: 300
            }
        },
        hide: {
            delay: 20,
            solo: true,
            effect: {
                type: 'fade',
                length: 500
            }
        }
    })

    //setup fake street view, hover-replace.
    $('img#fake-street-view').hover(function(){
        $(this)
            .hide()
            .after('<iframe width="560" height="350" frameborder="1" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.co.uk/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=Amethyst+Road,+Newcastle+Business+Park&amp;sll=53.766572,-0.266526&amp;sspn=0.009804,0.027874&amp;ie=UTF8&amp;hq=&amp;hnear=Amethyst+Rd,+Newcastle+Upon+Tyne+NE4+7,+United+Kingdom&amp;layer=c&amp;cbll=54.961445,-1.645095&amp;panoid=HqjQsJ7DhWmP_DzmsW8pJQ&amp;cbp=13,200.2,,0,-4.4&amp;ll=54.961106,-1.644682&amp;spn=0.000231,0.001338&amp;z=19&amp;iwloc=A&amp;output=svembed"></iframe>')
    });

    $('div#contact-box fieldset#submit button').click(function(){
       spinner=$(this).next('img.ajax-loader');
       $('div#contact-box fieldset#submit button')  .width($('div#contact-box fieldset#submit button').width()-spinner.width())
                                                    .text('Sending..')
       spinner.show();

       function display_contact_result(result,spinner){
            spinner.hide();
            $('div#contact-box fieldset#submit button').width($('div#contact-box fieldset#submit button').width()+spinner.width())

            contact_box = $('div#contact-box');
            success_box = $("<div></div>");

            if(result=='success'){
               html=$('<p id="contact-feedback-1">Thank You</p><p id="contact-feedback-2">for writing to me</p><p id="contact-feedback-3">I\'ll be in touch soon</p>');
               box=$('div#contact-box-success');
               $('div#contact-box fieldset#submit button').text('Sent :)');
            }
            else{
               html=$('<p id="contact-feedback-1">Sorry</p><p id="contact-feedback-2">there was a problem contacting the server</p><p id="contact-feedback-3">Would you like to ring me instead?</p>');
               box=$('div#contact-box-error');
               $('div#contact-box fieldset#submit button').text('Error :\'(');
            }
            box .css('height',contact_box.height())
                .css('width',contact_box.width())
                .css('position','absolute')
                .css('left',contact_box.position().left)
                .css('top',contact_box.position().top)
                .css('z-index','1100')
                .html(html)
                .show('highlight',{},1000,function(){
                    setTimeout(function(){
                            box.hide('blind',{},500);
                    }, 2000);
                });
       }
       
       form = $(this).closest('form');
       $.ajax({
              type: 'POST',
              url: "/wp-admin/admin-ajax.php",
              data: {name:   $("input[name='name']:first",form).val(),
                        email:  $("input[name='email']:first",form).val(),
                        phone:  $("input[name='phone']:first",form).val(),
                        comment: $("textarea[name='comment']:first",form).val(),
                        action: 'contact-form-ajax-submit'
              },
              success: function(){display_contact_result('success',spinner)},
              error: function(){display_contact_result('failure',spinner)}
        });
        return false;
    });

    $('div#contact-box form').submit(function(){return false;})
    $('a.focus-contact-form').click(function(){
        $.scrollTo( "#contact-box" , 1000 );
        $(this).show('transfer',{ to: "#contact-box", className: 'ui-effects-transfer' },1000,function(){
            $("input[name='name']:first",$('div#contact-box')).select();
        });
        
        return false;
    })


})

