/* Author: Peder Skeidsvoll (truben.no) */

var timer;var hashChange=false;var howmany=24;var loading=false;var firstRun=true;var haveMoreItems=true;var doChangeHash=true;$(document).ready(function(){addMenuMouseOvers('li.topMenu, li.subMenu,  li.subMenuItem-, #formAlign, span.shopButton, .notSelected');englishModalLink();facebookify('#fb_share_link',document.location.href);tweetify('#twitter_share_link',document.location.href);});function addPromoImageMouseOver(){var $title=$('#tableHack');if(getIEVersionNumber()<=7){$title.css('padding-top',0);$title.css('padding-bottom',0);}
$('#promoImage').hover(function(event){$title.stop().animate({"marginLeft":"0"},500);},function(){$title.stop().animate({"marginLeft":"-620"},600);});}
function getIEVersionNumber(){var ua=navigator.userAgent;var MSIEOffset=ua.indexOf("MSIE ");if(MSIEOffset==-1){return 0;}else{return parseFloat(ua.substring(MSIEOffset+5,ua.indexOf(";",MSIEOffset)));}}
function tweetify(selector,url){$(selector).hover(function(){$(selector).stop().animate({opacity:1},500)},function(){$(selector).stop().animate({opacity:0.5},500)});$(selector).attr('title','Del på Twitter').click(function(){return TweetAndTrack.open(this,url);});}
function facebookify(selector,url){$(selector).hover(function(){$(selector).stop().animate({opacity:1},500)},function(){$(selector).stop().animate({opacity:0.5},500)});$(selector).attr('title','Del på Facebook').click(function(){return facebookButton(url);});}
function facebookButton(url){window.open('http://www.facebook.com/sharer.php?u='+escape(url),'sharer','toolbar=0,status=0,width=626,height=450');return false;}
function englishModalLink(){var englishLink=$('#englishLink');englishLink.click(function(){$.get('/ajax/english/',function(data){englishLink.coolModalCreate($(data),500);$('#amount').bind('click change keyup',function(){initCurrencyCalculator();});$('#currency').change(function(){initCurrencyCalculator();});})
return false;});}
function setHashFragment(name,value){var hash=document.location.hash;if(hash.substring(0,1)=='#')
hash=hash.substring(1);var newHash="";var h=hash.split("&");var hasSet=false;for(i in h){var set=h[i].split("=");key=set[0];oldValue=set[1];if(key=='_'||oldValue==null)
continue;var delimeter="";if(i!=0)
delimeter="&";if(name==key){if(value!=null&&value!='')
newHash+=(delimeter+key+"="+value);hasSet=true;}
else
newHash+=(delimeter+key+"="+oldValue);}
if(!hasSet&&value!=''){var delimeter='&';if(hash==''||hash=='#'||hash=='#_'||hash=='_')
delimeter='';newHash+=delimeter+name+"="+value;}
changeHash(newHash);}
function addBoxesBottomMouseOvers(){$('.boxesBottom').each(function(index){var $title=$(this).find('.title');var $link=$(this).find('a');$link.hover(function(){$title.stop(true,true).slideDown('fast');},function(){$title.stop(true,true).slideUp();})});}
function initCurrencyCalculator(){$('#result').val(convertCurrency($('#amount').val(),$('#currency').val()))}
function checkShopHash(){if(hashChange)
return;var query=getHashFragment(document.location.hash,"q");var category=getHashFragment(document.location.hash,"category");var person=getHashFragment(document.location.hash,"person");var item=getHashFragment(document.location.hash,"shopItem");if(item||item!=''){showShopItem(item);return;}
else{var close=$('.coolModalClose');if(close.size()!=0){close.trigger('click');return;}}
if(query+category+person==''&&firstRun){firstRun=false;return;}
doSearch(query,category,person);}
function getHashFragment(hash,key){if(hash.substring(0,1)=='#')
hash=hash.substring(1);var h=hash.split("&");for(i in h){var set=h[i].split("=");if(set[0]==key)
return set[1];}
return"";}
function addLabelToInput(selector,text){var el=$(selector);searchElement=el;el.val(text);el.focus(function(){if(el.val()==text)
el.val('');});el.blur(function(){if(el.val()=='')
el.val(text);})}
function addMenuMouseOvers(selector){$(selector).hover(function(e){if($(e.currentTarget).hasClass('active')){$(e.currentTarget).attr('style','');return;}
$(this).stop().animate({opacity:1},200,function(){});},function(e){if($(e.currentTarget).hasClass('active')){$(e.currentTarget).attr('style','');return;}
$(this).stop().animate({opacity:0.5},250,function(){});});}
function changeHash(newHash){var oldHash=document.location.hash.replace('#','').replace('_','');newHash=newHash.replace('#','');if(!(oldHash==''&&newHash=='')&&newHash!=oldHash){hashChange=true;if(newHash==''&&document.location.hash!='')
window.location.hash="#_";else
window.location.hash=newHash;setTimeout(function(){hashChange=false;},100);}}
function fancySearch(query,category){clearTimeout(timer);timer=setTimeout(function(){doSearch(query,category)},300);}
function doSearch(query,category,person){startLoad();query=query||'';category=category||'';if($(document).data('category')!=category){haveMoreItems=true;howmany=24;}
$(document).data('category',category);var theUrl="";if(person)
theUrl='/ajax/category/?shopItems.navn.'+person+'=1';else
theUrl='/ajax/category/'+category+"?shopItems.query="+query+"&shopItems.limit="+howmany;$.ajax({url:theUrl,success:function(data){fancyTransition($('#shopGrid'),$(data).find('li'));if(person){changeHash('person='+person);markTitle('');}
else{changeHash(beautifyHash('category='+category+'&q='
+escape(query)+'&shopItem='
+getHashFragment(document.location.hash,"shopItem")));markTitle(category);}
if($(data).find('li').size()<howmany){haveMoreItems=false;}
stopLoad();},error:function(XMLHttpRequest,textStatus,errorThrown){document.location.href='/butikk/'+category
+'?shopItems.query='+escape(query);stopLoad();}});return false;}
function startLoad(){$('#ajaxLoader').stop().fadeIn();loading=true;}
function stopLoad(){$('#ajaxLoader').stop().fadeOut();loading=false;}
function showShopItem(itemId){startLoad()
$.get('/ajax/shopItem/'+itemId,function(data){var theData=$(data);theData.ready(function(){stopLoad();$(window).unbind('scroll');$('#shopItemId-'+itemId).coolModalCreate(theData,800,function(){setHashFragment('shopItem','');loadAtScroll();});$('#_isElement_shopItemImage').isSlideShow({'boxesElement':$('#shop-imageNavigator'),'activeImage':'/img/activeDot.png','inActiveImage':'/img/inactiveDot.png','picture':'Vis bilde','nextImage':'/img/nextCursor.png','loadImage':'/img/load-black.gif','baseUrl':"/butikk/-/"+itemId});facebookify('#fb_share_link',window.location.protocol+"//"+window.location.hostname+"/butikk/item/"+itemId);tweetify('#twitter_share_link',window.location.protocol+"//"+window.location.hostname+"/butikk/item/"+itemId);addMenuMouseOvers('span.shopButton');setHashFragment('shopItem',itemId);});})
return false;}
function showPerson(personName){$.get('/ajax/person/'+personName,function(data){$('#person-'+personName).coolModalCreate($(data),700);$('#_isElement_personImage').isSlideShow({'boxesElement':$('#shop-imageNavigator'),'activeImage':'/img/activeDot.png','inActiveImage':'/img/inactiveDot.png','picture':'Vis bilde','nextImage':'/img/nextCursor.png','loadImage':'/img/load-black.gif','baseUrl':"/om/knotere/"+personName});})
return false;}
function loadAtScroll(){$(window).scroll(function(){if(loading)
return;if($(window).scrollTop()>=$(document).height()
-$(window).height()-300&&haveMoreItems){howmany+=24;checkShopHash(true);}});}
function markTitle(name){var allMenues=$('.subMenu');var activeMenu=$('#subMenuItem-'+name);allMenues.removeClass('active');activeMenu.addClass('active');activeMenu.attr('style','');allMenues.attr('style','');}
function convertCurrency(amount,type){amount=amount.replace(",",".");type=type.toUpperCase();amount=amount/currencies['NOK'];return(amount*currencies[type]).toFixed(2);}
function beautifyHash(hash){var beautifiedHash='';fragments=hash.split('&');for(i in fragments){if(!endsWith(fragments[i],"="))
beautifiedHash+=fragments[i]+"&";}
return beautifiedHash.substring(0,beautifiedHash.length-1);}
function endsWith(s,end){try{if(s.substring(s.length-end.length)==end)
return true;return false}catch(e){return false;}}
function fancyTransition(element,newElements){element.quicksand(newElements,{duration:300,easing:'easeInOutQuad'},function(){$('li.shopItemList').attr('style','');});}
function doesInclude(query1,query2){query2=query2.toLowerCase();var q=query1.split(' ');for(var i=0;i<q.length;i++){if(query2.search(q[i])==-1)
return false;}
return true;}
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"
+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}
$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}
$(f.start)},teardown:function(){if(d){return false}
$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}
p=setTimeout(n,$.fn[c].delay)}
$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t
+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);
