jQuery(document).ready(function() {
	jQuery(".item.try_block_click").removeClass('try_block_click').addClass('hover_enabled');
	
	jQuery(".item.hover_enabled").click(function() {
		if(jQuery(this).find('a').first().length == 1)
			window.location = jQuery(this).find('a').first().attr('href');
	});

	
	if(jQuery("article .gallery_image").length > 0 && jQuery("aside.portfolio").length > 0) {
		jQuery("article .gallery_image").each(function(index) {
			var cssclass = 'inactive';
			if(index==0) cssclass='active';
			jQuery("aside.portfolio").append(	''+
												'<div class="gallery_image '+cssclass+' '+jQuery(this).attr('class')+'">'+
												'<a href="javascript:showImage('+index+');">'+
												'<img src="'+jQuery(this).find('img.thumb').attr('src')+'" alt="" />'+
												'</a>'+
												'</div>'
											);
		});
	}

	jsonEnable();
	
});

function jsonEnable() {
	jQuery(".json a").each(rewriteURL);
	checkForHashChange();
}
function log() {
	if (window.console && window.console.log)
		window.console.log('debug: ' + Array.prototype.join.call(arguments,' '));
}

function checkForHashChange(){
	var prevHash = '';
	if(window.location.hash.indexOf('#!/')!=-1) {
		openPage(window.location.hash.substr(3));
	}

	if (("onhashchange" in window) && !(jQuery.browser.msie)) {
		jQuery(window).bind( 'hashchange',function(e) {
			if (window.location.hash != prevHash) {
				prevHash = window.location.hash;
				if(window.location.hash.indexOf('#!/')!=-1) {
					openPage(window.location.hash.substr(3));
				}
			}
		});
	} else {
		window.setInterval(function () {
			if (window.location.hash != prevHash) {
				prevHash = window.location.hash;
				if(window.location.hash.indexOf('#!/')!=-1) {
					openPage(window.location.hash.substr(3));
				}
			}
		}, 100);
    }
}

function rewriteURL() {
	var href = jQuery(this).attr("href");
	var parts = href.split('/');
	var the_url = parts[parts.length-2];
	if(the_url!='portfolio') {
		jQuery(this).attr("href",'#!/'+the_url);
	}
	
	var json_href = href + '?json=1';
}

function openPage(the_url) {
	var json_href = '/' +the_url + '?json=1';
	
	jQuery("nav ul li").removeClass('current-menu-item');
	jQuery(".removefromjson").remove();
	jQuery('body').removeClass('home').removeClass('portfolio');
	jQuery('aside.under').show();
	jQuery('nav ul li a[href$="'+the_url+'"]').parent().addClass('current-menu-item');
	
	jQuery("#loader").fadeIn(500);
	jQuery("#pagecontainer").slideUp(500,function () {
		jQuery.getJSON(json_href,function(result) {
			jQuery("#loader").fadeOut(500);
			if(result.status == 'ok') {
				if(result.page) { // requested a page
					jQuery("#pagecontainer .wrapper").html('');
					jQuery("#pagecontainer .wrapper").removeClass('big_image').addClass('big_image').css({"background-image": "url('"+result.page.bgimage+"')"});
					jQuery("#pagecontainer .wrapper").append(
															'<div id="content">'+
																'<div id="post-'+result.page.id+'">'+
																	'<h2 class="entry-title depfont"></h2>'+
																	'<h3 class="entry-subtitle depfont"></h3>' +
																	'<div class="entry-content"></div>' +
																'</div>'+
															'</div>');



					document.title = 'Design Electro Products >> '+result.page.title;
					if(result.page.colouredtitle) {
						jQuery("h2.entry-title").html(result.page.colouredtitle);
					} else {
						jQuery("h2.entry-title").html(result.page.title);
					}
					jQuery("h3.entry-subtitle").html(result.page.subtitle);
					jQuery("div.entry-content").html(result.page.content);
					jQuery("#pagecontainer").slideDown(1000);
				}
			}
		});
	});
	
	
}

function showImage(index) {
	jQuery("aside .gallery_image").removeClass('active').eq(index).addClass('active');
	jQuery("article .gallery_image").hide().eq(index).show();
	var target_offset = jQuery("header h2.entry-title").offset();
	var target_top = target_offset.top;
	jQuery("html, body").scrollTop(target_top);
}

