//>> Initialize various enhancements
$(document).ready(function(){
    if ($("#id_copy_address:checked").val() == "on") {
        $("#shipping-address").hide()
    }

    //hiding shipping address in the checkout
    $("#id_copy_address").click(function(){        
        if ($("#id_copy_address:checked").val() == "on") {
            $("#shipping-address").slideUp()
        }
        else {
             $("#shipping-address").slideDown()
        }
    });

    //display slideSet styles only if JavaScript is available
    $("input.slideSet").addClass('slideSetStyle');
});


$(document).ready( function() {
   
    $('a[@rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
   
});


/**
 * Clear the clicked text input
 */

Client = {
  // focus function
  inputFocus: function() {
    if ( this.value == jQuery('#search_help').get(0).value ) {
      this.value = '';
    }
  },

  // blur function
  inputBlur: function() {
    if (this.value == '') {
      this.value = jQuery('#search_help').get(0).value;
    }
  },

  go: function() {
    // tie the two functions to the Javascript
    jQuery('#search_keywords').focus(Client.inputFocus
      ).blur(Client.inputBlur
      // Run the blur function to put the text there
      ).blur();
  }
};

// Run the Keyword.go() function when the page is ready
jQuery(document).ready(Client.go);

/**
 * Clickable categories
 */
$(function(){ 
    $('#childcats li').click(function(){ 
        location.href = $('a', this).attr('href'); 
    }); 
});


/*
Insert Flash movies
*/
$(function(){ 
    $('#childcats li').click(function(){ 
        location.href = $('a', this).attr('href'); 
    }); 
});





/****************
 * Old school JS
 */


/*
DOM library by Riki "Fczbkk" Fridrich
http://www.fczbkk.com/js/dom/
*/

// library for cross-browser event management
evt = {

	// attach event
	add : function(obj, evType, fn, useCapture) {
		// Opera hack
		if (window.opera && (obj == window)) {
			obj = document;
		}
		
		if (obj.addEventListener){
			obj.addEventListener(evType, fn, useCapture);
			return true;
		} else if (obj.attachEvent){
			var r = obj.attachEvent("on"+evType, fn);
			return r;
		} else {
			return false;
		}
	},
	
	// remove event
	remove : function(obj, evType, fn, useCapture) {
		// Opera hack
		if (window.opera && (obj == window)) {
			obj = document;
		}
		
		if (obj.removeEventListener) {
			obj.removeEventListener(evType, fn, useCapture);
			return true;
		} else if (obj.detachEvent) {
			var r = obj.detachEvent("on"+evType, fn);
			return r;
		} else {
			return false;
		}
	},
	
	// fix for IE event model
	fix : function(e) {
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.target == 'undefined') e.target = e.srcElement;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		if ((typeof e.which == 'undefined') && e.keyCode) e.which = e.keyCode;

		// thanx to KKL2401 for preventDefault hack
		if (!e.preventDefault) e.preventDefault = function() {
			e.returnValue = false;
		}

		return e;
	}

}



// library for working with multiple classes
var cls = {
	
	// vrati pole obsahujuce vsetky triedy daneho elementu
	get : function (elm) {
		if (elm && elm.tagName) {
			var classes = [];
			if (elm.className) {	// na zaklade Centiho upozornenia o divnej interpretacii v Opere
				var cl = elm.className.replace(/\s+/g, " ");
				classes = cl.split(" ");
			}
			return classes;
		}
		return false;
	},
	
	// vrati true, ak element obsahuje triedu
	has : function (elm, cl) {
		if ((actCl = cls.get(elm)) && (typeof(cl) == "string")) {
			for (var i = 0; i < actCl.length; i++) {
				if (actCl[i] == cl) {
					return true;
				}
			}
		}
		return false;
	},
	
	// prida triedu elementu
	add : function (elm, cl) {
		if ((actCl = cls.get(elm)) && (typeof(cl) == "string")) {
			if (!cls.has(elm, cl)) {
				elm.className += (actCl.length > 0) ? " " + cl : cl;
			}
			return true;
		}
		return false;
	},
	
	// odstrani triedu z elementu
	remove : function (elm, cl) {
		if ((actCl = cls.get(elm)) && (typeof(cl) == "string")) {
			tempCl = "";
			for (var i = 0; i < actCl.length; i++) {
				if (actCl[i] != cl) {
					if (tempCl != "") {tempCl += " ";}
					tempCl += actCl[i];
				}
				elm.className = tempCl;
			}
			return true;
		}
		return false;
	},
	
	// nahradi staru triedu elementu novou, ak stara neexistuje, prida novu
	replace : function (elm, oldCl, newCl) {
		if ((actCl = cls.get(elm)) && (typeof(oldCl) == "string") && (typeof(newCl) == "string")) {
			tempCl = "";
			if (cls.has(elm, newCl)) {
				cls.remove(elm, oldCl);
			} else if (cls.has(elm, oldCl)) {
				for (var i = 0; i < actCl.length; i++) {
					if (tempCl != "") {tempCl += " ";}
					tempCl += (actCl[i] == oldCl) ? newCl : actCl[i];
				}
				elm.className = tempCl;
			} else {
				cls.add(elm, newCl);
			}
			return true;
		}
		return false;
	}

}

/*
slideSet
http://www.fczbkk.com/js/slide_set/
*/

slideSet = {
	
// default
increment : 1,
sensitivity : 10,

// icon
icon : "slide_set.gif",

// vars
startingPoint : null,
startingValue : null,
activeElm : null,

init : function (elm, increment, sensitivity, min, max, trans1, trans2) {
    if (elm) {
        // TODO: kontrola typu elementu
        if (!increment) {increment = slideSet.increment;}
        if (!sensitivity) {sensitivity = slideSet.sensitivity;}
   
        // vytvorime ikonu
        // pozn.: Ikona nemoze byt obrazok, robi to problemy pri odchytavani eventu. Vhodnejsi by bol DIV.
        var newIcon = document.createElement("div");

        newIcon.slideSetElm = elm;
        elm.slideSetElm = elm;
        
        elm.slideSetIncrement = increment;
        elm.slideSetSensitivity = sensitivity;
        elm.slideSetMin = min;
        elm.slideSetMax = max;
        elm.trans1 = trans1;
        elm.trans2 = trans2;
        // zavesime na ikonu akcie
        evt.add(newIcon, "mousedown", slideSet.start);
        evt.add(elm, "mousedown", slideSet.start);


    }
    return false;
},

    start : function(e) {
        e = evt.fix(e);
        evt.add(document, "mouseup", slideSet.end);
        evt.add(document, "mousemove", slideSet.move);
        
        var elm = e.target.slideSetElm;
        if (elm) {
            slideSet.activeElm = elm;
            slideSet.startingPoint = e.clientY;
            slideSet.startingValue = elm.value;
            //debug("starting point: " + slideSet.startingPoint);
            return true;
        }
        return false;
    },
    
    end : function(e) {
        $("#carttable").before("<div id=\"alert\"></div>");
        
        e = evt.fix(e);
        evt.remove(document, "mouseup", slideSet.end);
        evt.remove(document, "mousemove", slideSet.move);
        slideSet.setValue(e.clientY);
        //debug("starting point: " + slideSet.startingPoint + "<br />skoncil som: " + e.clientY);
    },
    
    move : function(e) {
        e = evt.fix(e);
        slideSet.setValue(e.clientY);
        //debug("starting point: " + slideSet.startingPoint + "<br />hybem sa: " + e.clientY);
    },
    
    setValue : function(pos) {
        var elm = slideSet.activeElm;
        var pnt = slideSet.startingPoint;
        var val = slideSet.startingValue;
        // TODO: validacia vstupov
        if (isNaN(val)) {
                val = 0;
        }
        var newVal = 0;
        newVal = (val * 1) - (Math.round((pos - pnt) / elm.slideSetSensitivity) * elm.slideSetIncrement);
        if (!isNaN(elm.slideSetMax) && (newVal > elm.slideSetMax)) {newVal = elm.slideSetMax;}
        if (!isNaN(elm.slideSetMin) && (newVal < elm.slideSetMin)) {newVal = elm.slideSetMin;}
        elm.value = newVal;
        
        if (elm.value == elm.slideSetMax) {debug( elm.trans1 + elm.slideSetMax + elm.trans2 );};
    },
    
    nothing : function(e) {
        e = evt.fix(e);
        e.preventDefault();
    }
	
}

function debug(txt) {
    //document.getElementById("alert").innerHTML = txt;
    $("#alert").empty().append(txt)
    .slideDown('slow')
    .animate({opacity: 1.0}, 3000)
    .slideUp('slow', function() {
        $(this).remove();
    });
}

