// JavaScript Document

var w = Math.round(($(window).width() - 80)/2);
var color2 = '#8C9091';
var color1 = '#000000';
var color3 = '#231F20';
var height = 497;
var iC = 0;
var rootURL = 'http://www.graphicalhouse.co.uk';
var pageTitle = 'Graphical House';
var prevCategory = '';
var newsletterForm = '<form action="http://nsmail.nsdesign.co.uk/t/r/s/djdlki/" method="post" id="subForm"><input type="text" name="cm-djdlki-djdlki" id="djdlki-djdlki" class="email" value="Your email" /><input class="submit" type="submit" value="/ Submit" /></form>';

var socialLinks = ''
+ '<a href="http://www.facebook.com/sharer.php" onclick="window.open('+"'http://www.facebook.com/sharer.php?u='+encodeURIComponent(location.href.split('/#/').join('/'))+'&t='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;" + '">Facebook</a> / '
+ '<a href="http://twitter.com/share" onclick="window.open('+"'http://twitter.com/share/?url='+encodeURIComponent(location.href.split('/#/').join('/'))+'&text='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;" + '">Twitter</a> / '
+ '<a href="http://www.delicious.com/save" onclick="window.open('+"'http://www.delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href.split('/#/').join('/'))+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;" + '">Delicious</a>'
;


$(document).ready(function(){
	actions()
	makeAjax();
	$(window).resize(onResize);
	onResize();
	$('.closebtn a').click(goBackFromClick);
	rollovers()
	$('#newsletter-form').hover(showNewsletterForm, hideNewsletterForm);
});

function showNewsletterForm() {
	//console.log($(this));
	if (!$(this).hasClass('j')) {
		$(this).addClass('j');
		$(this).hide().fadeIn(500);
		$(this).html(newsletterForm);
		$('#newsletter-form .email').focus(function() { $(this).val(''); });
		$('#newsletter-form').submit(submitNewsletterForm);
	}
}

function submitNewsletterForm() {
$.ajax({
   type: "GET",
   url: $('#newsletter-form form').attr('action'),
   data: $('#newsletter-form form').serialize(),
   success: function(msg){
   	$('#newsletter-form .submit').val('/ Done');
     //alert( "Data Saved: " + msg );
   }
 });
	return false;
}

function hideNewsletterForm() {}

function rollovers() {
	$('.menu a, .submenu a').addClass('j');
	$('.menu a, .submenu a').hover(function() {
		if (!$(this).parent().hasClass('currentpage')) { $(this).stop().css({color:color3}).animate({color:color2}, 250); }
	}, function() {
		if (!$(this).parent().hasClass('currentpage')) { $(this).stop().css({color:color2}).animate({color:color3}, 250); }
	})
}

function goBackFromClick() {
	$.address.value(prevCategory);
	return false;
}

function onResize() {
	if ($('.grid').length > 0) {
		$('.grid').width($(document).width() - 30);
	}
	if ($(window).height() <  600 || true) {
		$('#footer').hide();
	} else {
		$('#footer').show();
	}
}
$.address.change(function(event) {
	if (event.value == '/shop') {
		location.href = 'http://www.graphicalshop.com/';
	} else if ($.address.baseURL() == rootURL || location.href.indexOf('#') != -1) {    	
    	$('#content').stop().animate({opacity:0}, {duration:150});
    	$('#loading').delay(100).fadeIn(100);
    	if (event.value == '/blog') { 
    		loadBlog(); 
    	} else {
    		$('#content').load(rootURL + event.value + ' #content .inside', stuffLoaded);
    	}
    	//scrollToAnchor('header', 0);
    	$("html").scrollTop(0);
    } else if (($.address.baseURL() != rootURL || location.href.indexOf('#') != -1 ) && event.value != '/' ) {
    	location.href = rootURL + '/#' + event.value;
    } 
    
});
function isProjectPage() {
	return $.address.value().indexOf('projects') == 1 && $.address.value().length > 9;
}
function parseUrl() {
	$('li.currentpage').removeClass('currentpage');
	var selector = 'a[href=' + rootURL + $.address.value() + ']';
	$(selector).parent().addClass('currentpage')
	$(selector).stop().animate({color:color2}, 250);; 
	
	$('#menus li a').not('li.currentpage a').css({color:color3})
	
	if ($.address.value() == '/' || $.address.value().indexOf('?tag') != -1) {
		onResize();
		prevCategory = $.address.value();
	} else if ($.address.value() == '/blog') {
		$('.blogentry .blog_desc p').each(function() {
			if($(this).text().split(' ').join('') == '') { $(this).hide(); }
		});
		/*
		$('.blogentry .blog_desc img').each(function() {
			if ($(this).parent().parent().is('p') && $(this).parent().parent().text().split(' ').join('') == '') { $(this).parent().parent().hide(); }
		});
		*/
	} 
}
function actions() {
	startGrid();
	makeGridAjax();
	moreText();
	
	/* for galleries */
	galleryAnchors();
	$('#content').animate({opacity:1},{duration:150});
	$('#loading').stop().clearQueue().hide();
	titleVisabilityTest();
	if ($('.gallery').length > 0) {
		$('.content').css({'margin-top':$('.left_col_content').height()/*+3*/-0 + 'px', 'position':'fixed', 'width': '247px'});
		$('.more_content_col2').css('margin-top', parseInt($('.content').css('margin-top'))+7);
		//$('html,body').scrollTop($('.gallery').offset().top - 190);
		$($('#content .anchors a')[0]).trigger('click');
		
	}
	if (hideLeftConContent()) { $('.left_col_content').hide(); } else { $('.left_col_content').show(); }
	socialLinksAction();
}

function hideLeftConContent() {
	 if ($('.grid').length > 0) { return true } 
	 else {
	 	switch ($.address.value()) {
	 		case "/blog":
	 		case "/shop":
	 		case "/clients":
	 		case "/profile":
	 			return true;
	 		break;
	 		default:
	 			return false;
	 	}
	 } 
	 return false;
}

function titleVisabilityTest() {
	if ($.address.value().indexOf('projects') != -1 && ($.address.value().length == 9 || $.address.value().indexOf('?tag') != -1 )) {
    	$('#content h1').hide();
    } else {
    	$('#content h1').show();
    	if (($.address.value().indexOf('/projects/') == 0 && $.address.value().length > 10) || ($('.gallery').length)) {
    		$('.left_col_content').css({'background':'#FFFFFF none repeat scroll 0 0','left':'15px','padding-top':'4px','position':'fixed','top':'191px'});
		} else {
			$('.left_col_content').attr('style','');
		}
    }
}

function makeAjax() {
	$('#menus a').not('a[href=http://graphicalshop.com/]').click(function() {
		$.address.value($(this).attr('href').split(rootURL).join(''));  
		//$('#content').load($(this).attr('href') + ' #content .inside', stuffLoaded)
		return false;
	});
}
function makeGridAjax() {
	$('.grid a').click(function() {
		//$('#content').load($(this).attr('href') + ' #content .inside', stuffLoaded)
		$.address.value($(this).attr('href').split(rootURL).join(''));  
		return false;
	});
}

function stuffLoaded() {
	//$('html,body').css({scrollTop: 0}); 
	actions();
	$.address.title(pageTitle + ' - ' + $($('#content h1')[0]).text());
	parseUrl()
    if (isProjectPage()) {
    	$('.inside').addClass('project');
    	$('.closebtn').fadeIn();
    } else if($('.gallery').length) { 
    	$('.inside').addClass('project');
    	$('.closebtn').hide();
    } else {
    	$('.inside').removeClass('project');
    	$('.closebtn').hide();
    }
}

function scrollToAnchor(anchor, position) {
	anchor = anchor.split(' ').join('_');
	if(anchor == 'a_Read_more...') { position = 0;}
	if ($("#" + anchor).length > 0) { 
		if (position) {} else {
			position = $("#" + anchor).offset().top - 200;
		}
		$('html,body').animate({scrollTop: position}); 
	} else if (position || position == 0) {
		$('html,body').animate({scrollTop: position}); 
	}
	return false;
}

function galleryAnchors() {
	var anchorsA = [];
	$('a.anchor').each(function() {
		anchorsA.push('<a href="' + $(this).attr('title').split(' ').join('_') + '">' + $(this).attr('name') + '</a>')
	});
	$('#content .anchors').html(anchorsA.join(', '));
	if ($('a.anchortxt').length > 0) {
		//anchorsA.push('<a href="' + $('a.anchortxt').attr('title').split(' ').join('_') + '">Read more...</a>')
		$('.more_content').show();
		$('.content').append('<p><span class="anchors more_more_anchor"><a id="read_more_btn" href="' + $('a.anchortxt').attr('title').split(' ').join('_') + '">Read more...</a><span></p>');
	}
	$('#content .anchors a').click(function() { scrollToAnchor('a_' + $(this).text()); return false });
	
	$('.gototop a').click(function() { scrollToAnchor('content'); return false });
	$('.group:last').parent().addClass('last');
	/*
	$('.group').each(function(index){
		$(this).click(function() {  if( (index+1) != $('.group').length) { scrollToAnchor($($('.group')[(index+1)]).find('img').attr('id')) } else {}; return false;})
	});
	*/
	//$('.img a').click(function() { return false; });
	$('.img a').each(function(index){
		$(this).click(function() {  if( (index+1) != $('.img a').length) { scrollToAnchor($($('.img')[(index+1)]).attr('id')) } else {}; return false;})
	});
	
}

function moreText() {
	$('.more_content').hide();
	if ($('.more_content').text() != '') {
		$('.content').append('<a href="#" class="show_more">/ More</a>');
		$('.show_more').click(function() {  $('.more_content').slideDown(350);
											$(this).hide();
											return false;
											})
	}
}

/* PROJECT FUNCTIONS */

function menuRollOver() {
	$(this).animate({color:color1});
}

function menuRollOut() {
	$(this).clearQueue() 
	if ($(this).hasClass('selected')) {
		//$this.animate({color:"#776538"}); 
	} else {
		$(this).animate({color:color2}); 
	}
}
function startGrid() { 
	$('.grid').hide().delay(100).queue(grid);
}
function grid() {
	$('.grid').show();
	$('.grid li').css({display:'none'}).addClass('j');
	$('.grid li .thumb_color').css({opacity:0});
	$('.grid li').bind('mouseenter', function() {
								 	$(this).find('a').stop().css({color:color1}).animate({color:color1}, 250);
								 	$(this).find('.thumb_color').show().stop().animate({opacity:1}, 250);
								 })
	$('.grid li').bind('mouseleave', function() {
									$(this).find('a').stop().css({color:color2}).animate({color:color2}, 250);
									$(this).find('.thumb_color').stop(true).animate({opacity:0}, {duration:250, complete:hideMe, queue:false})//.hide();
								 })
	items = $('.grid li');
	items.each(function(index) {		
		$(this).delay(100*index).queue(showGridItemAnim);
		$(this).dequeue()
  	});
}

function hideMe() {
	$(this).hide();
}

function showGridItemAnim() {
	$(this).fadeIn();
}

function loadBlog() {
	$('#content').load('http://www.graphicalhouse.co.uk/blog/ #wrapper', onBlogLoaded);

}

function onBlogLoaded() {
	stuffLoaded();
	$('#wrapper').find('#content').attr('id', 'content_blog');
	$('#wrapper').find('#header').html('').hide();
	$('#wrapper').find('#footnotes').html('').hide();
	$('#wrapper a').attr('target', '_blank');

/* infinity scroll */

(window.INFSCR_jQ ? jQuery.noConflict() : jQuery)(function($){
  
  // Infinite Scroll jQuery+Wordpress plugin
  $('#content').infinitescroll({
    debug           : false,
    nextSelector    : "div.navigation a:first",
    loadingImg      : "http://www.graphicalhouse.co.uk/GH/blog/wp-content/plugins/infinite-scroll/ajax-loader.gif",
    loadingText     : "<em>Loading the next set of posts...</em>",
    donetext        : "<em>Congratulations, you've reached the end of the internet.</em>",
    navSelector     : "div.navigation",
    contentSelector : "#content_blog",
    itemSelector    : "#content > div.post"
    },function(){ 
 
    });
});	
	
}

function socialLinksAction() {
	if ($('.gallery').length) {
		if ($('.more_more_anchor').length) {
			$('.more_more_anchor').prepend('<a id="share_btn" href="#share">Share</a> / ').addClass('share_c');
		} else {
			$('.content').append('<p class="share_c anchors"><a id="share_btn" href="share">Share</a> /</p>');
		}
		$('.share_c').append('<br/><span class="share_links" style="display:none">'+socialLinks+'<span>');
		$('#share_btn').click(function() { $('.share_links').fadeIn(); return false; });
		//$('.content').append('<p><span class="anchors more_more_anchor"><a id="read_more_btn" href="' + $('a.anchortxt').attr('title').split(' ').join('_') + '">Read more...</a><span></p>');
	}
}

