/*
KNGLY//STRIKINGLY ORIGINAL
by cornerstone interactive
cornerstoneinteractive.com
@cornerstone - twitter.com
*/

// Get this party started!
jQuery(document).ready(function($){
  kY();
  $(window).resize(function(){kY();});
  $('#smoothnav li a').xFont({fontSize:10});
  $('.wordtitle').xFont({fontSize:12, color:'00AEEF'});
  $('#smoothnav li.divider').xFont({fontSize:10});
  $('#kollection').xFont({fontSize:10});
  $('#kategories').cycle({
    fx: 'scrollHorz',
    timeout:0,
    pager:'#katnav',
    pagerAnchorBuilder: function(idx, slide) { 
      var title = $(slide).find('.kategory').attr('id');
      return '<a href="/produkts/' + title + '">' + title + '</a>';
    }
  });
  $('#katnav a').xFont({fontSize:8});

  ajaxnav();
  loadPage();
  
});


function knglyTabs() {

	//When page loads... 
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("rel"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
}

function fixPSizes() {
  $('#optBtns a').each(function(){
    if($(this).text()=="xxs"){$(this).animate({backgroundPosition: '2px 50%'});}
    if($(this).text()=="xs"){$(this).animate({backgroundPosition: '3px 50%'});}
    if($(this).text()=="s"){$(this).animate({backgroundPosition: '4px 50%'});}
    if($(this).text()=="m"){$(this).animate({backgroundPosition: '1px 50%'});}
    if($(this).text()=="l"){$(this).animate({backgroundPosition: '4px 50%'});}
    if($(this).text()=="xl"){$(this).animate({backgroundPosition: '4px 50%'});}
  });
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


function kY() {
  var kY = $(window).height()-($('#kontainer').offset().top);
  $('#kontainer').height(kY);
  $('#kontent').height(kY);
  $('#kategories').height(kY);
  $('.jScrollPaneContainer').height(kY);
  $('.kategory').jScrollPane(); 
}

function loadPage(url) {
  $('kontainer').append('<div id="progress">Loading...</div>');
  if (url != undefined) {
      $('#kontent').load(url+' #inner', function(data) {
        ajaxlinks();
        $('#optBtns a').xFont({fontSize:12,color:'00aeef',bgColor:'000000'});
        //$('.pNum').xFont({font:'Helvetica.ttf',fontSize:90,color:'00AEEF',bgColor:'808080',kern:-20});
        fixPSizes();
        if (parseFloat($('#cartCount').text())>0){$('#cartCount').css('display','block');}else{$('#cartCount').css('display','none');}
      });
  }else{
    ajaxlinks();
    $('#optBtns a').xFont({fontSize:12,color:'00aeef',bgColor:'000000'});
    //$('.pNum').xFont({font:'Helvetica.ttf',fontSize:90,color:'00AEEF',bgColor:'808080',kern:-20});
    fixPSizes();
        if (parseFloat($('#cartCount').text())>0){$('#cartCount').css('display','block');}else{$('#cartCount').css('display','none');}
  }

}
function ajaxnav() {
  $('#smoothnav a').click(function(e){
      e.preventDefault();
      loadPage($(this).attr('href'));
  });
  $('#kategories a').click(function(e){
      e.preventDefault();
      loadPage($(this).attr('href'));
  });
  $('#progress').remove();
}

function ajaxlinks() {
  $('#kontent #inner').jScrollPane();
  kY();
    $('h2 a').each(function(){
    if (IsNumeric($(this).text())){$(this).addClass('pNum');};
  });
  pP();
  
  $('#optBtns a').click(function(e){
    e.preventDefault();
    // $.get($(this).attr('href'),function(response){
      
    // });
    
    $.ajax({ url: $(this).attr('href'), success: function(data){
          $('#cartCount').text(parseFloat($.cookie("kart"))+1);
          $('#cartCount').css('display','block');
      }});
  });
  // $('#kontent a').click(function(e){
      // e.preventDefault();
      // loadPage($(this).attr('href'));
  // });
  // $('.button').click(function(e){
      // e.preventDefault();
      // $('#kontent').load($(this).parents('form').attr('action') +' #inner', ajaxlinks);
  // });
  $('#progress').remove();
    knglyTabs();
}

function pP(){
  $('.pp_pic_holder').remove();
  $('.pp_overlay').remove();
  $('.ppt').remove();
  
          $("a[rel^='wp-prettyPhoto']").prettyPhoto({
            animationSpeed: 'normal',
            padding: 2,
            opacity: 0.5,
            showTitle: false,
            allowresize: true,
            counter_separator_label: '/',
            theme: 'dark_square',
            wmode: 'opaque',
            hideflash: false,
            modal: false,
            changepicturecallback: function(){},
            callback: function(){},
            image_markup: '<img id="fullResImage" src="" />',
            flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
            quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
            iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
            inline_markup: '<div class="pp_inline clearfix">{content}</div>'
          });
}

/**
 * @author Alexander Farkas
 * v. 1.21
 */


(function($) {
	if(!document.defaultView || !document.defaultView.getComputedStyle){ // IE6-IE8
		var oldCurCSS = jQuery.curCSS;
		jQuery.curCSS = function(elem, name, force){
			if(name === 'background-position'){
				name = 'backgroundPosition';
			}
			if(name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[ name ]){
				return oldCurCSS.apply(this, arguments);
			}
			var style = elem.style;
			if ( !force && style && style[ name ] ){
				return style[ name ];
			}
			return oldCurCSS(elem, 'backgroundPositionX', force) +' '+ oldCurCSS(elem, 'backgroundPositionY', force);
		};
	}
	
	var oldAnim = $.fn.animate;
	$.fn.animate = function(prop){
		if('background-position' in prop){
			prop.backgroundPosition = prop['background-position'];
			delete prop['background-position'];
		}
		if('backgroundPosition' in prop){
			prop.backgroundPosition = '('+ prop.backgroundPosition;
		}
		return oldAnim.apply(this, arguments);
	};
	
	function toArray(strg){
		strg = strg.replace(/left|top/g,'0px');
		strg = strg.replace(/right|bottom/g,'100%');
		strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
		var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
		return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
	}
	
	$.fx.step. backgroundPosition = function(fx) {
		if (!fx.bgPosReady) {
			var start = $.curCSS(fx.elem,'backgroundPosition');
			
			if(!start){//FF2 no inline-style fallback
				start = '0px 0px';
			}
			
			start = toArray(start);
			
			fx.start = [start[0],start[2]];
			
			var end = toArray(fx.options.curAnim.backgroundPosition);
			fx.end = [end[0],end[2]];
			
			fx.unit = [end[1],end[3]];
			fx.bgPosReady = true;
		}
		//return;
		var nowPosX = [];
		nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
		nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
		fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

	};
})(jQuery);

//xFont plugin for jQuery
$.fn.xFont=function(opts){var defaults={path:'/img/xFont.php',textIndent:-1000,font:'BlairMdITC.ttf',fontSize:12,width:0,height:0,color:'ffffff',hoverColor:'000000',kern:0,bgColor:'000000'};var opts=$.extend(defaults, opts);return this.each(function(){obj=$(this);imageText=obj.text();if(opts.width==0){textLength=imageText.length;width=Math.round((opts.fontSize)*(textLength)+10);}else{width=opts.width;}if(opts.height == 0){height=opts.fontSize * 1.5;}else{height=opts.height;}path=jQuery.trim(opts.path);url=path+'?text='+encodeURIComponent(imageText) +'&font='+opts.font+'&size='+opts.fontSize+'&color='+opts.color+'&kern='+opts.kern+'&bgcolor='+opts.bgColor;obj.addClass("xFont");obj.css({'width':width+'px','height':height+'px','background-image':'url('+url+')','text-indent':opts.textIndent});$('head').append('<style>.xFont{background-repeat:no-repeat;background-position: 0 50%; overflow: hidden;padding:0px;}</style>');});};