/*
Author: Arno Richter
URL: http://arnorichter.de/
Created: 2011-03-16
Modified: 2011-07-16
*/

var oelna = window.oelna || {};
oelna.vehemenz = window.oelna.vehemenz || {};


$(document).ready(function() {
	
	/* smarter category links */
	$('.page-home dd, .page-category dd').each(function() {
		var link = $(this).parent().find('a').attr('href');
		$(this).click(function() {
			window.location.href = link;
		});
	});
	
	$('#logo a').click(function() {
		localStorage.setItem('category', 'all');
	});
	
	if($('html').is('.iphone, .android')) {
		//actions for mobile devices
		
		//active phone numbers (not needed due to smart iphone parsing)
		//var phone = $('#info').find(":contains('tel')");
		
		
	} else {
		//actions for desktop browsers
		
		//whitespace tech for the homepage
		if($('.page-home').length > 0) {
			$('.page-home dl').each(function() {
				var category;
				var link = $(this).find('dt a').attr('href').split('/');
				if(link[1]) { category = link[1]; } else { category = ''; }
				
				$(this).addClass('category-'+category);
			});
			
		}
		$('.select').click(function(e) { //make the main navigation dynamic
			var category = $(this).attr('rel');
			localStorage.setItem('category', category); //save the currently active category to localstorage
			if($('.page-home').length > 0) {
				$('.select').parent().removeClass('active');
				$(this).parent().addClass('active'); //make the link active
				if(category != '') {
					$('dl dt, dl dd').show();
					$('dl').not('.category-'+category).find('dt, dd').hide();
					e.preventDefault();
				}
				
				//make the logo a dynamic link
				$('#logo a').addClass('dynamic-home');
			} else {
				window.location.href = '/';
				e.preventDefault();
			}
		});
		$('.dynamic-home').live('click', function(e) {
			//show all categories again
			$('dl dt, dl dd').show();
			//make all navigation inactive again
			$('.select').parent().removeClass('active');
			e.preventDefault();
		});
		
		//if an active category was saved, reactivate it
		if($('.page-home').length > 0) {
			if(localStorage.getItem('category')) {
				var category = localStorage.getItem('category');
				if(category != '' && category != 'all') {
					$('dl').not('.category-'+category).find('dt, dd').hide();
					$('.select[rel="'+category+'"]').parent().addClass('active'); //make the nav link active as well
				}
				
				//make the logo a dynamic link
				$('#logo a').addClass('dynamic-home');
			}
		}
	
	}
	
	$('#footer-nav #link-back').click(function(e) {
		window.history.back(1);
		e.preventDefault();
	});
	
	//soft scrolling
	$('#footer-nav #link-top').click(function(e) {
		e.preventDefault();
		$('html, body').animate({scrollTop: 0}, 600);
	});
	
	//make double or triple image columns targetable
	$('.page-set dt').each(function() {
		var images = $(this).find('img');
		if(images.length > 1) {
			images.addClass('multi-image multi-image-'+images.length);
		} else {
			images.addClass('single-image');
		}
	});
});

//enable localstorage for old browsers
if(!window.localStorage) {
  Object.defineProperty(window, "localStorage", new (function () {
    var aKeys = [], oStorage = {};
    Object.defineProperty(oStorage, "getItem", {
      value: function (sKey) { return this[sKey]; },
      writable: false,
      configurable: false,
      enumerable: false
    });
    Object.defineProperty(oStorage, "key", {
      value: function (nKeyId) { return aKeys[nKeyId]; },
      writable: false,
      configurable: false,
      enumerable: false
    });
    Object.defineProperty(oStorage, "setItem", {
      value: function (sKey, sValue) {
        if(!sKey) { return; }
        document.cookie = escape(sKey) + "=" + escape(sValue) + "; path=/";
      },
      writable: false,
      configurable: false,
      enumerable: false
    });
    Object.defineProperty(oStorage, "length", {
      get: function () { return aKeys.length; },
      configurable: false,
      enumerable: false
    });
    Object.defineProperty(oStorage, "removeItem", {
      value: function (sKey) {
        if(!sKey) { return; }
        var sExpDate = new Date();
        sExpDate.setDate(sExpDate.getDate() - 1);
        document.cookie = escape(sKey) + "=; expires=" + sExpDate.toGMTString() + "; path=/";
      },
      writable: false,
      configurable: false,
      enumerable: false
    });
    this.get = function () {
      var iThisIndx;
      for (var sKey in oStorage) {
        iThisIndx = aKeys.indexOf(sKey);
        if (iThisIndx === -1) { oStorage.setItem(sKey, oStorage[sKey]); }
        else { aKeys.splice(iThisIndx, 1); }
        delete oStorage[sKey];
      }
      for (aKeys; aKeys.length > 0; aKeys.splice(0, 1)) { oStorage.removeItem(aKeys[0]); }
      for (var iCouple, iKey, iCouplId = 0, aCouples = document.cookie.split(/;\s*/); iCouplId < aCouples.length; iCouplId++) {
        iCouple = aCouples[iCouplId].split("=");
        if (iCouple.length > 1) {
          oStorage[iKey = unescape(iCouple[0])] = unescape(iCouple[1]);
          aKeys.push(iKey);
        }
      }
      return oStorage;
    };
    this.configurable = false;
    this.enumerable = true;
  })());
}
