var origContent = "";
var origTitle = "";
var origBodyClass = "";

function toHide(id){
	$('#'+id).hide();
}
function loadAjax(href){

	if(href != ''){

		if(origContent == ""){
			origContent = $('#container').html();
			origTitle = $('title').html();
			origBodyClass = $('body').attr('class');
		}

		// Load the content
		$('#container').load(href + ' #main', function(dahtml){
			$('title').load(href + ' title');

			// Get the body class
			var dabody = dahtml.match(/(?:.*\<body.*class\=\")([^"]+)(?:\".*)/ig);
			var daclass = dabody.toString().match(/(no-js)\s([^"]+)/i);

			$('body').attr('class', daclass.toString().split(',')[0])
			.removeClass('no-js')
			.addClass('has-js');

			// Show new content
			$('#container').animate({
				'opacity': '1'
			}, 300);

			// And init placeholder JS fallback
			$('input[placeholder]').placeholder();

			locatorInit();

		});

	}
	else if(origContent != ""){
		$('#container').html(origContent);
		$('title').html(origTitle);
		$('body').attr('class', origBodyClass);
	}

}



// Reseller gallery
function resellerGallery(){
	if($('#reseller-popup').length > 0){

		// Close popup
		$('#reseller-popup').find('a.close').live('click', function(e){
			e.preventDefault();

			$('#reseller-popup').fadeOut(300, function(){
				$('#reseller-overlay').fadeOut(200, function(){
					$('#reseller-overlay, #reseller-popup').remove();
				});
			});
		});

		var $gallery = $('#reseller-popup ul.gallery'),
			$place = $('#reseller-popup img.gallery-place');

		$gallery.find('li a').live('click', function(e){
			e.preventDefault();
			var $link = $(this);

			$place.animate({'opacity': 0}, 500, function(){
				$place.attr('src', $link.attr('href'));
				$place.delay(200).animate({'opacity': 1}, 500);
			});
		});
	}
}

function locatorSubmit(){
	$('#locator-results').slideUp(200, function(){
		$('#locator-results').load('includes/storelocator-results.php', {
			'query': $('#locator-query').val()
			}, function(){
				$('#locator-results').slideDown(300);
			});
		$('#gmaps').attr('src','map.php?address='+$('#locator-query').val());
	});
}

function locatorInit(){
	if($('#locator').length === 1){
		$('#locator-results').hide();

		$('#locator').find('form').live('submit', function(e){
			e.preventDefault();
			locatorSubmit();
		});

		$('#locator-results a.details').live('click', function(e){
			e.preventDefault();

			$('<div />', {
				'id' : 'reseller-popup',
				'style' : 'display:none'
			}).appendTo('#main');

			$('<div />', {
				'id': 'reseller-overlay',
				'style' : 'display:none'
			}).appendTo('#main')

			$('#reseller-popup').load($(this).attr('href') + ' #reseller-content', function(){
				$('#reseller-overlay').fadeTo(200, 0.3, function(){
					$('#reseller-popup').fadeIn(500);
					resellerGallery();
				});
			});
		});
	}
}





var startup = function(){

	$.history.init(loadAjax, {unescape: "/?&=,"});

	// Let CSS know we are JS capable
	$('body').removeClass('no-js').addClass('has-js');
	if($.browser.msie && $.browser.version < 8){
		$('html').addClass('old-ie');
	}

	// Header slide
	if($.cookie('pemat_slide_header') != 'ran'){
		var hheight = $('#head').outerHeight();
		$('#head').css('top', '-' + hheight + 'px').delay(700).animate({
			top: 0
		}, 500, function(){
			$.cookie('pemat_slide_header', 'ran', {
				path: '/'
			});
		});
	}



	// HTML5 Placeholder fallback
	$.fn.placeholder = function(){

		return this.each(function(){

			if(!('placeholder' in document.createElement(this.tagName.toLowerCase()))){
				var $this = $(this),
				placeholder = $(this).attr('placeholder');

				$this.addClass('placeholder').val(placeholder);

				$this.focus(function(){
					if($this.val() == placeholder){
						$this.removeClass('placeholder').val('');
					}
				});

				$this.blur(function(){
					if($this.val() == '' || $this.val() == placeholder){
						$this.addClass('placeholder').val(placeholder);
					}
				});
			}

		});

	}

	// Init placeholder JS fallback
	$('input[placeholder]').placeholder();



	// Drop-down menu
	if($('#side-tree').length !== 0){
		$('#side-tree li ul').hide();
		$('#side-tree a').click(function(e){
			e.preventDefault();
			$this = $(this);
			$('#side-tree a').removeClass('active');
			$this.toggleClass('active').siblings('ul').slideToggle(300);
			$this.parent('li').siblings('li').find('ul').slideUp(300);
		});
	}


	// Ajaxified links
	$('.ajaxify a, a.ajaxify').live('click', function(e){

		var href = $(this).attr('href');

		// Prevent click
		e.preventDefault();

		if($(this).attr('onclick') !== null){
			return;
		}

		// Activate the link and deactivate siblings
		$(this).parent().addClass('active').siblings().removeClass('active');

		// Hide old content
		$('#container').animate({
			'opacity': '0'
		}, 300, function(){
			var url = href;
			url = url.replace(/^.*#/, '');

			if(window.location.hash.replace(/^.*#/, '') == url){
				loadAjax(url);
				return;
			}

			$.history.load(url);

		});

	});



	// CSS3 Support detection
	var supports = (function() {
		var div = document.createElement('div'),
		vendors = 'Khtml Ms O Moz Webkit'.split(' '),
		len = vendors.length;

		return function(prop) {
			if ( prop in div.style ) return true;

			prop = prop.replace(/^[a-z]/, function(val) {
				return val.toUpperCase();
			});

			while(len--) {
				if ( vendors[len] + prop in div.style ) {
					return true;
				}
			}
			return false;
		};
	})();

	if(supports('transform')){
		$('body').addClass('has-transform');
	}



	// Popups
	$('a.popup-link').live('click', function(e){
		e.preventDefault();
		var $popup = $('#' + $(this).attr('rel')),
		offset = $(this).offset(),
		$link = $(this);

		$popup.slideToggle(300);

		if($popup.hasClass('popup-top')){
			$popup.offset({
				top: offset.top + + $link.outerHeight() + 20,
				left: offset.left
				});
		}
		if($popup.hasClass('popup-left')){
			$popup.offset({
				top: offset.top - 20,
				left: offset.left + $link.outerWidth() + 20
				});
		}
		if($popup.hasClass('popup-from-right')){
			$popup.offset({
				top: $popup.offset().top,
				left: offset.left + $link.outerWidth() - $popup.outerWidth() + 7
				});
		}

	});

	locatorInit();

}

// INIT
$(startup);
