//document.domain = "swarovski-crystallized.com";

String.prototype.startsWith = function(str) {
	return (this.match("^"+str)==str);
};

/*
 *
 * Copyright (c) Christian Sporer (http://www.acriba.at)
 */
 
/*
 * jQuery Image Replacement. An alternative to using CSS hacks
 * The id attribute (or class) is used for the filename
 *
 * @name     IR
 * @param    String path      (optional) Path to PHP file for dynamic replacement
 * @author   Christian Sporer (http://www.acriba.at)
 * @example  $(".ir").IR("/ir/heading.php");
 * @before   <h1 id="heading1" class="ir">Heading 1</h1>
 *           <h2 class="ir">Heading 2</h2>
 * @result   <h1 id="heading1" class="ir"><img alt="Heading 1" src="images/heading1.png"></h1>   
 *           <h2 class="ir"><img alt="Heading 2" src="images/heading2.png"></h2>    
 */
jQuery.fn.IR = function(path) {
	if(!document.images) {
		el = jQuery(this);
		el.show();
		return this;
	}
	path = path || "ir.php";
	this.each(
		function() {
			var img = $("<img>"), el = jQuery(this);			
			var file = path + "&text="+ escape(utf8_encode(el.text())) + "&class=" + escape(utf8_encode(el.attr('class')));
			
			jQuery(img).attr({
				src: file,
				alt: el.text()
			});
			var a = el.find("a");
			var toAppend = a.length ? a.empty().append(img) : img;
			el.empty().append(toAppend);
			el.show();
		}
	)
	return this;
}

jQuery.fn.fitImage = function(options) {
	var fit = {
		all : function(imgs){
			imgs.each(function(){
				fit.one(this);
				})
			},
		one : function(img){
			$(img)
				.width('100%').each(function() {
					if($(this).width() > $(this).attr('startwidth')) {
						$(this).width($(this).attr('startwidth'));
					}
					$(this).height(Math.round(
						$(this).attr('startheight')*($(this).width()/$(this).attr('startwidth')))
					);
				})
			}
	};
	
	this.each(function(){
		var container = this;
			
		// store list of contained images (excluding those in tables)
		var imgs = $('img', container).not($("table img"));
				
		// store initial dimensions on each image 
		imgs.each(function(){
			$(this).attr('startwidth', $(this).width())
				.attr('startheight', $(this).height())
				.css('max-width', $(this).attr('startwidth')+"px");

			fit.one(this);
		});
		// Re-adjust when window width is changed
		$(window).bind('resize', function(){
			fit.all(imgs);
		});
	});
	
	return this;
}

// $Id: jquery_csspngfix.src.js,v 1.0 2008/06/14 18:29:32 skilip Exp $
jQuery.fn.cssPNGFix = function(sizingMethod) {
  var sizingMethod = (sizingMethod) || 'scale';
  if($.browser.msie && parseInt($.browser.version) < 7) {
    $(this).each(function() {
      var css_bg_img = $(this).css('background-image');
      var bg_img = css_bg_img.substring(5, css_bg_img.length - 2);
      $(this).css({'background-image':'none', 'filter':'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + bg_img + '", sizingMethod="' + sizingMethod + '")'});
    });
  };
  return $(this);
};
jQuery.fn.imgPNGFix = function(sizingMethod) {
  var sizingMethod = (sizingMethod) || 'scale';
  if($.browser.msie && parseInt($.browser.version) < 7) {
    $(this).each(function() {
      var spacer_src = 'static/img/pngPixel.gif';
      var the_img = $(this);
      var src = $(this).attr('src');
      $(the_img).attr('src', spacer_src);
      $(the_img).css({'background-image':'none', 'filter':'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="' + sizingMethod + '")'});
    });
  };
  return $(this);
};

/* Wayfarer Tooltip
 * Version 1.0.4
 * Author Abel Mohler
 * URI: http://www.wayfarerweb.com/wtooltip.php
 * Released with the MIT License: http://www.wayfarerweb.com/mit.php
 */
(function(a){a.fn.wTooltip=function(f,r){f=a.extend({content:null,ajax:null,follow:true,auto:true,fadeIn:0,fadeOut:0,appendTip:document.body,degrade:false,offsetY:10,offsetX:1,style:{},className:null,id:null,callBefore:function(t,p,o){},callAfter:function(t,p,o){},clickAction:function(p,o){a(p).hide()},delay:0,timeout:0,cloneable:false},f||{});if(!f.style&&typeof f.style!="object"){f.style={};f.style.zIndex="1000"}else{f.style=a.extend({border:"1px solid gray",background:"#edeef0",color:"#000",padding:"10px",zIndex:"1000",textAlign:"left"},f.style||{})}if(typeof r=="function"){f.callAfter=r||f.callAfter}f.style.display="none",f.style.position="absolute";var m,l,q,c,k={},b=true,e=false,n=false,s=document.createElement("div"),g=(typeof document.body.style.maxWidth=="undefined")?true:false,j=(typeof a.talk=="function"&&typeof a.listen=="function")?true:false;if(f.id){s.id=f.id}if(f.className){s.className=f.className}f.degrade=(f.degrade&&g)?true:false;for(var d in f.style){s.style[d]=f.style[d]}function i(o){if(o){if(f.degrade){a(s).html(f.content.replace(/<\/?[^>]+>/gi,""))}else{a(s).html(f.content)}}}if(f.ajax){a.get(f.ajax,function(o){if(o){f.content=o}i(f.content)})}function h(p){function t(u){if(m&&!f.content){u.title=m;m=null}}function o(){if(!e&&f.auto){clearInterval(c);if(f.fadeOut){a(s).fadeOut(f.fadeOut,function(){t(p)})}else{t(p);s.style.display="none"}}if(typeof f.callAfter=="function"){f.callAfter(s,p,f)}if(j){f=a.listen(f)}}if(f.timeout>0){q=setTimeout(function(){o()},f.timeout)}else{o()}}a(s).hover(function(){e=true},function(){e=false;h(k)});if(j){f.key=s;f.plugin="wTooltip";f.channel="wayfarer";a.talk(f)}i(f.content&&!f.ajax);a(s).appendTo(f.appendTip);return this.each(function(){this.onmouseover=function(t){var p=this;clearTimeout(q);if(this.title&&!f.degrade&&!f.content){m=this.title;this.title=""}if(f.content&&f.degrade){this.title=s.innerHTML}function o(){if(typeof f.callBefore=="function"){f.callBefore(s,p,f)}if(j){f=a.listen(f)}if(f.auto){var u;if(f.content){if(!f.degrade){u="block"}}else{if(m&&!f.degrade){a(s).html(unescape(m));u="block"}else{u="none"}}if(u=="block"&&f.fadeIn){a(s).fadeIn(f.fadeIn)}else{s.style.display=u}}}if(f.delay>0){l=setTimeout(function(){o()},f.delay)}else{o()}};this.onmousemove=function(y){var w=(y)?y:window.event,v=this;k=this;if(f.follow||b){var A=a(window).scrollTop(),B=a(window).scrollLeft(),x=w.clientY+A+f.offsetY,t=w.clientX+B+f.offsetX,z=a(f.appendTip).outerHeight(),p=a(f.appendTip).innerHeight(),o=a(window).width()+B-a(s).outerWidth(),u=a(window).height()+A-a(s).outerHeight();x=(z>p)?x-(z-p):x;n=(x>u||t>o)?true:false;if(t-B<=0&&f.offsetX<0){t=B}else{if(t>o){t=o}}if(x-A<=0&&f.offsetY<0){x=A}else{if(x>u){x=u}}s.style.top=x+"px";s.style.left=t+"px";b=false}};this.onmouseout=function(){clearTimeout(l);var o=this;b=true;if(!f.follow||n||(f.offsetX<0&&f.offsetY<0)){setTimeout(function(){c=setInterval(function(){h(o)},1)},1)}else{h(this)}};if(typeof f.clickAction=="function"){this.onclick=function(){f.clickAction(s,this)}}})}})(jQuery);

/* timeout-cleaner */
var to = new Array();
function ct(n) {
	clearTimeout(to[n]);
}

/* formfield-default-value-toggle */
function inputDefaultToggle(field, condtxt, valtxt) {
	if (field.value == condtxt) {
		field.value = valtxt;
		if (condtxt=='') $(field).addClass('faded');
	} else {
		$(field).removeClass('faded');
	}
}

/* nav-drop-downs */
var actNav = '';
function navDropDown(id, state) {
	if (state=="on") {
		if (actNav!='') navDropDown(actNav, 'off');
		$('#'+id).addClass('hover');
		if (id=='meta-1') $('#'+id+'-drop').show();
		else $('#'+id+'-drop').fadeIn(200);
		actNav = id;
	}
	if (state=="off") {
		$('#'+id).removeClass('hover');
		$('#'+id+'-drop').hide();
		if (actNav==id) actNav = '';
	}
}

function getScroll (){
	if (self.pageYOffset) {
		yPos = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yPos = document.documentElement.scrollTop;
	} else if (document.body) {
		yPos = document.body.scrollTop;
	}
}

function setScroll (x, y){
	window.scrollTo(x, y);
}

function clickSubscribe() {
	var myClose=function(hash) { 
		var wasSuccess = $('.dialogInfo').length != 0;
		hash.w.hide();
		hash.o.remove(); 
		if(wasSuccess == true) {
			$('#nlemail').val('Your E-Mail');
		} else {
			$('#nlemail').focus();
		}
	}; 
	var email = $('#nlemail').val();
	$('#subscribeResult').jqm({toTop: true, modal: true, ajax: '/?eID=newsletterSubscribe&email=' + encodeURIComponent(email), trigger: false, onHide: myClose});
	$('#subscribeResult').jqmShow();
}

function changeDeliveryAddress() {
	if ($("#shippingAddressDifferent:checked").val() == "1") {
		if($('#shippingAddressTable').hasClass('disabled')) {
			$('#shippingAddressTable').removeClass('disabled');
		}
		$('#gender_female').removeAttr('disabled');
		$('#gender_male').removeAttr('disabled');
		$('#firstname').removeAttr('disabled');
		$('#lastname').removeAttr('disabled');
		$('#address1').removeAttr('disabled');
		$('#address2').removeAttr('disabled');
		$('#postcode').removeAttr('disabled');
		$('#city').removeAttr('disabled');
		$('#state').removeAttr('disabled');
	} else {
		$('#shippingAddressTable').addClass('disabled');
		$('#gender_female').attr("disabled", "disabled");
		$('#gender_male').attr("disabled", "disabled");
		$('#firstname').attr("disabled", "disabled");
		$('#lastname').attr("disabled", "disabled");
		$('#address1').attr("disabled", "disabled");
		$('#address2').attr("disabled", "disabled");
		$('#postcode').attr("disabled", "disabled");
		$('#city').attr("disabled", "disabled");
		$('#state').attr("disabled", "disabled");
	}
}

//private method for UTF-8 encoding
function utf8_encode (string) {
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

		var c = string.charCodeAt(n);

		if (c < 128) {
			utftext += String.fromCharCode(c);
		}
		else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		}
		else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}

	}

	return utftext;
}

function showPopup( htmlFile, wWindow, hWindow ) {
	var lWindow = (screen.width-wWindow)/2;
	var tWindow = (screen.height-hWindow)/2;
	window.open(htmlFile,'popup','toolbar=no,top=' + tWindow + 'location=no,left='+ lWindow + 'directories=no,status=no,menubar=no,resizable=yes,scrollbars=no,width=' + wWindow + ',height=' + hWindow);
}

function showImagePopup(elem) {
	imgPreloader = new Image();
            //var mOffset = $('#modalWindow').show().offset();

            //$.log("mWidth =" + mWidth);
            //$.log("mHeight =" + mHeight);
            imgPreloader.onload = function(){
                imgPreloader.onload = null;
                var imageWidth = imgPreloader.width;
                var imageHeight = imgPreloader.height;
                var w = $(window).width() - 100;
                var h = $(window).height() - 100;
								
                if (imageWidth > w) {
                    imageHeight = imageHeight * (w / imageWidth);
                    imageWidth = w;
                    if (imageHeight > h) {
                        imageWidth = imageWidth * (h / imageHeight);
                        imageHeight = h;
                    }
                }
                else
                    if (imageHeight > h) {
                        imageWidth = imageWidth * (h / imageHeight);
                        imageHeight = h;
                        if (imageWidth > w) {
                            imageHeight = imageHeight * (w / imageWidth);
                            imageWidth = w;
                        }
                    }
                                        var mWinWidth = parseFloat(imageWidth);
                                        var mWinHeight = parseFloat(imageHeight);
                                        var mWinTop = parseFloat((h- imageHeight))/2;
                                        var mWinLeft = parseFloat((w-imageWidth))/2;
                
				
                $img = "<a href='#' class='jqmClose'><img id='TB_Image' src='" + this.src + "'width='" + imageWidth + "' height='" + imageHeight + "' alt='' border='0' /></a>";
				$('<div id="imageDetailsPopup" class="jqmWindow"></div>').appendTo('body');
				$('#imageDetailsPopup').html($img);
				
				$('#imageDetailsPopup').css({
                                        margin: 0,
                                        width: mWinWidth + 'px',
                                        height: mWinHeight+'px',
                                        top: mWinTop+'px',
                                        left: mWinLeft + 'px'
                                });
				$('#imageDetailsPopup').jqm();
				$('#imageDetailsPopup').jqmShow();
            }
            imgPreloader.src = $(elem).attr("href");
            return false;
}

function recordOutboundLink(link, ip, uri, name) {
  dcsMultiTrack('DCS.dcssip', ip, 'DCS.dcsuri', uri, 'WT.ti', name);
  try {
    var pageTracker=_gat._getTracker("UA-4588732-4");
    pageTracker._trackEvent('Offsite', ip + uri);
    setTimeout('document.location = "' + link.href + '"', 100)
  }catch(err){}
}
