/*var ge_top = 0;
var ge_left = 0;
$('body, #content').width($('#content').width()); */

function scrollWindow()
{
    window.scrollBy(900,0);
}
function scrollbackWindow()
{
    window.scrollBy(-900,0);
}

$('.contentWrap, .contentWrap_about, .contentWrap_web, .pane, .tooltip, .toggle_container').hide();

$(function(){
    $("a[rel]").overlay({
        mask: '#333',
        effect: 'apple',
        onBeforeLoad: function() {
            // grab wrapper element inside content
            var wrap = this.getOverlay().find(".contentWrap, .contentWrap_about, .contentWrap_web");

            // load the page specified in the trigger
            wrap.load(this.getTrigger().attr("href"));
        },
        onLoad: function(e) {
            var el = e.originalTarget || e.srcElement;
            var id = $(el).attr( 'rel' );
            $(id + ' img').trigger("appear");
        }
    });

    $(".scrollable, .scrollable-web").scrollable({
        mousewheel: false
    });

    /*   $(window).scroll(function() {
        ge_top = $(window).scrollTop();
        ge_left = $(window).scrollLeft();
    });*/
  
    $('.feed-news').rssfeed('http://www.graphic-evidence.co.uk/blog/category/news/feed/', {
        limit: 3,
        header: false,
        date: false,
        linktarget: '_blank'
    });
	
	
    /* TOGGLE */
	
    $(".tel").hover(function () {
        $(".toggle_container").toggle("slow");

    });
	
		
   
    /* LAZY LOAD */
    $("img:not(.notlazy)").lazyload({
        placeholder : "images/white.gif",
        effect      : "fadeIn"
    });

    $("#main-area").Caro({
        btnNext: ".page_next",
        btnPrev: ".page_prev",
        btnGo: [".0", ".1", ".2",".3",".4"]
    });
    if ($.browser.msie && $.browser.version == 6.0) {
        $(".page_next").fadeIn().css("left",$(document).width()-154+"px");
    }	else $(".page_next").fadeIn().css("left",$(document).width()-130+"px");
		
	
 /*   Accordian 
    $('#accordion > h2').click(function(e) {
        e.preventDefault();
        $(this).siblings('div').hide();
        $(this).next().show();
    }); /*


    /* VALIDATION ON CONTACT FORM */
    $("#ContactForm1").validate();


    /* TOOL TIP */
    $(".plaque-tt a[title]").tooltip({
        events: {
            def: "click,mouseout"
        },
        effect: 'fade'
    });
    /* TOOL TIP */
    $(".arrow-r a[title]").tooltip({
        events: {
            def: "mouseover,mouseout"
        },
        effect: 'fade'
    });
	
	    /* TOOL TIP */
    $("#accordion > h2").tooltip({ 
        events: {
            def: "click,mouseout", 
			opacity: 0.7
			},
        effect: 'fade'
}).dynamic({ bottom: { direction: 'down', bounce: true } });;

    /*TOGGLE */
    $("button").click(function () {
        $("#toggleContactForm").toggle("slow");
    });
	
    /*GOOGLE */
    if($('#gmap').length){
        var centerCoord = new google.maps.LatLng(51.62057451128889, 0.009613037109375);
        var myOptions = {
            zoom: 10,
            center: centerCoord,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: false
        }
        map = new google.maps.Map(document.getElementById("gmap"), myOptions);
		
        var mymarker = new google.maps.MarkerImage(
        'images/marker.png',
        new google.maps.Size(40,40),
        new google.maps.Point(0,0),
        new google.maps.Point(20,40)

    );
        var shadow = new google.maps.MarkerImage(
        'images/marker-shadow.png',
        new google.maps.Size(64,40),
        new google.maps.Point(0,0),
        new google.maps.Point(20,40)

    );
        var marker = new google.maps.Marker({
            icon: mymarker,
            shadow: shadow,
            position: new google.maps.LatLng(51.7266, 0.2315),
            map: map
        });
        var marker = new google.maps.Marker({
            icon: mymarker,
            shadow: shadow,
            position: new google.maps.LatLng(51.5147388, -0.0872873),
            map: map
        });
    }

/* HOMEPAGE SCROLLER */

    $('#pause').click(function() { $('#latestwork').cycle('pause'); return false; });
    $('#play').click(function() { $('#latestwork').cycle('resume'); return false; });
    
    $('#latestwork-hd').hover(
    function() {
        $('#controls').fadeIn();
    },
    function() {
        $('#controls').fadeOut();
    }
);
    $('#latestwork').cycle({
        fx: 'scrollLeft',
        speed: 600,
        timeout: 2000
    });

    $('.featured-web').cycle({
        fx: 'fade'
    });
	
	
    /* TWITTER FEED */	
	
    if ($(".twitter-feed1").length > 0){
        $.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=graphicevidence&count=4&callback=?",
        function(data){
            $.each(data, function(i,item){
                var created_time = toISOString(new Date(item.created_at));
                ct = "<div class='twtr-tweet-text'>" + item.text;
                ct = ct.replace(/http:\/\/\S+/g,  '<a href="$&" target="_blank">$&</a>');
                ct = ct.replace(/\s(@)(\w+)/g,    ' @<a href="http://twitter.com/$2" target="_blank">$2</a>');
                ct = ct.replace(/\s(#)(\w+)/g,    ' #<a href="http://search.twitter.com/search?q=%23$2" target="_blank">$2</a>');
                $(".twitter-feed1").append(ct + '<br/><a class="twtr-timestamp" href="http://twitter.com/graphicevidence/status/' + item.id + '">' + jQuery.timeago(created_time) + '</a>' + ' <a class="twtr-reply" href="http://twitter.com/?status=@graphicevidence&in_reply_to_status_id='+item.id+'&in_reply_to=graphicevidence" target="_blank">reply</a></div>');

            });
        });
    }

	
});

// create custom animation algorithm for jQuery called "drop"
$.easing.drop = function (x, t, b, c, d) {
    return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
};

// loading animation
$.tools.overlay.addEffect("drop", function(css, done) {
    // use Overlay API to gain access to crucial elements
    var conf = this.getConf(),
    overlay = this.getOverlay();

    // determine initial position for the overlay
    if (conf.fixed)  {
        css.position = 'fixed';
    } else {
        css.top += ge_top;
        css.left += ge_left;
        css.position = 'absolute';   
    }

    // position the overlay and show it
    overlay.css(css).show();

    // begin animating with our custom easing
    overlay.animate({
        top: '+=55',
        opacity: 1,
        width: '+=20'
    }, 600, 'drop', done);

    /* closing animation */
}, function(done) {
    this.getOverlay().animate({
        top:'-=55',
        opacity:0,
        width:'-=20'
    }, 500, 'drop', function() {
        $(this).hide();
        done.call();
    });
}
);



function MM_openBrWindow(theURL,winName,features) { //v2.0
    window.open(theURL,winName,features);
}

window.fbAsyncInit = function() {
    FB.init({
        appId: 'your app id',
        status: true,
        cookie: true,
        xfbml: true
    });
};
(function() {
    var e = document.createElement('script');
    e.async = true;
    e.src = document.location.protocol +
        '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
}());

function toISOString(d) {
    return d.getUTCFullYear() + '-' +  padzero(d.getUTCMonth() + 1) + '-' + padzero(d.getUTCDate()) + 'T' + padzero(d.getUTCHours()) + ':' +  padzero(d.getUTCMinutes()) + ':' + padzero(d.getUTCSeconds()) + '.' + pad2zeros(d.getUTCMilliseconds()) + 'Z';
}
function padzero(a){
    return a<10?"0"+a:a
}
function pad2zeros(a){
    if(a<100){
        a="0"+a
    }
    if(a<10){
        a="0"+a
    }
    return a
}

/* AJAX FORM */
$(document).ready(function() {
	
	//if submit button is clicked
	$('#submit').click(function () {		
		
		//Get the data from all the fields
		var name = $('input[name=name]');
		var company = $('input[name=company]');
		var email = $('input[name=email]');
		var phone = $('input[name=phone]');
		var comment = $('textarea[name=comment]');

		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		if (name.val()=='') {
			name.addClass('hightlight');
			return false;
		} else name.removeClass('hightlight');
		
		if (email.val()=='') {
			email.addClass('hightlight');
			return false;
		} else email.removeClass('hightlight');
		
		if (phone.val()=='') {
			phone.addClass('hightlight');
			return false;
		} else phone.removeClass('hightlight');
		
		if (comment.val()=='') {
			comment.addClass('hightlight');
			return false;
		} else comment.removeClass('hightlight');
		
		//organize the data properly
		var data = 'name=' + name.val() + '&company=' + company.val() + '&email=' + email.val() + '&phone=' + phone.val() + '&comment='  + encodeURIComponent(comment.val());
		
		//disabled all the text fields
		$('.text').attr('disabled','true');
		
		//show the loading sign
		$('.loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "process.php",	
			
			//GET method is used
			type: "GET",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {				
				//if process.php returned 1/true (send mail success)
				if (html==1) {					
					//hide the form
					$('.form').fadeOut('slow');					
					
					//show the success message
					$('.done').fadeIn('slow');
					
				//if process.php returned 0/false (send mail failed)
				} else alert('Sorry, unexpected error. Please try again later.');				
			}		
		});
		
		//cancel the submit button default behaviours
		return false;
	});	
});	


