/* 
 * Cross-browser event handling, by Scott Andrew
 */
function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

/* 
 * Kills an event's propagation and default action
 */
function knackerEvent(eventObject) {
    if (eventObject && eventObject.stopPropagation) {
        eventObject.stopPropagation();
    }
    if (window.event && window.event.cancelBubble ) {
        window.event.cancelBubble = true;
    }
    
    if (eventObject && eventObject.preventDefault) {
        eventObject.preventDefault();
    }
    if (window.event) {
        window.event.returnValue = false;
    }
}

/* 
 * Safari doesn't support canceling events in the standard way, so we must
 * hard-code a return of false for it to work.
 */
function cancelEventSafari() {
    return false;        
}

/* 
 * Cross-browser style extraction, from the JavaScript & DHTML Cookbook
 * <http://www.oreillynet.com/pub/a/javascript/excerpt/JSDHTMLCkbk_chap5/index5.html>
 */
function getElementStyle(elementID, CssStyleProperty) {
    var element = document.getElementById(elementID);
    if (element.currentStyle) {
        return element.currentStyle[toCamelCase(CssStyleProperty)];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(element, '');
        return compStyle.getPropertyValue(CssStyleProperty);
    } else {
        return '';
    }
}

/* 
 * CamelCases CSS property names. Useful in conjunction with 'getElementStyle()'
 * From <http://dhtmlkitchen.com/learn/js/setstyle/index4.jsp>
 */
function toCamelCase(CssProperty) {
    var stringArray = CssProperty.toLowerCase().split('-');
    if (stringArray.length == 1) {
        return stringArray[0];
    }
    var ret = (CssProperty.indexOf("-") == 0)
              ? stringArray[0].charAt(0).toUpperCase() + stringArray[0].substring(1)
              : stringArray[0];
    for (var i = 1; i < stringArray.length; i++) {
        var s = stringArray[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1);
    }
    return ret;
}

/*
 * Disables all 'test' links, that point to the href '#', by Ross Shannon
 */
function disableTestLinks() {
  var pageLinks = document.getElementsByTagName('a');
  for (var i=0; i<pageLinks.length; i++) {
    if (pageLinks[i].href.match(/[^#]#$/)) {
      addEvent(pageLinks[i], 'click', knackerEvent, false);
    }
  }
}

/* 
 * Cookie functions
 */
function createCookie(name, value, days) {
    var expires = '';
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        var expires = '; expires=' + date.toGMTString();
    }
    document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
    var cookieCrumbs = document.cookie.split(';');
    var nameToFind = name + '=';
    for (var i = 0; i < cookieCrumbs.length; i++) {
        var crumb = cookieCrumbs[i];
        while (crumb.charAt(0) == ' ') {
            crumb = crumb.substring(1, crumb.length); /* delete spaces */
        }
        if (crumb.indexOf(nameToFind) == 0) {
            return crumb.substring(nameToFind.length, crumb.length);
        }
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, '', -1);
}

addEvent(window, 'load', init, false);

function init() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bsearch_input\b/)) {  
            /* Add event handlers */          
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}
function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}
//Sky Gallery Script By Michael
function skyGallery(type,id,num,rez){
	if(type=='swf'){
		var code = "<embed src='/product/" + id + "/p" + num + "-" + id + ".swf' width='390' height='316' />"
		document.getElementById('skyGallery').innerHTML = code;
	}else if(type=='jpg'){
		if(rez==1){
			var code = "<a href='/product/" + id + "/p" + num + "-" + id + ".jpg' class='highslide' onclick='return hs.expand(this)'><img src='/product/" + id + "/p" + num + "-" + id + ".jpg' width='440' height='320' /></a>"
		}else{
			var code = "<img src='/product/" + id + "/p" + num + "-" + id + ".jpg' width='440' height='320' />"
		}
		document.getElementById('skyGallery').innerHTML = code;
	}
	
}

//Fatty Drop Down
$(document).ready(function() {

	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}


	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 50, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#navigation li .sub").css({'opacity':'0'});
	$("ul#navigation li").hoverIntent(config);



});

//Front Slider
$(document).ready(function() {

            //Set Default State of each portfolio piece
            $(".paging").show();
            $(".paging a:first").addClass("active");
                
            //Get size of images, how many there are, then determin the size of the image reel.
            var imageWidth = $(".window").width();
            var imageSum = $(".image_reel img").size();
            var imageReelWidth = imageWidth * imageSum;
            
            //Adjust the image reel to its new size
            $(".image_reel").css({'width' : imageReelWidth});
            
            //Paging + Slider Function
            rotate = function(){	
                var triggerID = $active.attr("rel") - 1; //Get number of times to slide
                var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

                $(".paging a").removeClass('active'); //Remove all active class
                $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
                
                //Slider Animation
                $(".image_reel").animate({ 
                    left: -image_reelPosition
                }, 500 );
                
            }; 
            
            //Rotation + Timing Event
            rotateSwitch = function(){		
                play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
                    $active = $('.paging a.active').next();
                    if ( $active.length === 0) { //If paging reaches the end...
                        $active = $('.paging a:first'); //go back to first
                    }
                    rotate(); //Trigger the paging and slider function
                }, 7000); //Timer speed in milliseconds (3 seconds)
            };
            
            rotateSwitch(); //Run function on launch
            
            //On Hover
            $(".image_reel a").hover(function() {
                clearInterval(play); //Stop the rotation
            }, function() {
                rotateSwitch(); //Resume rotation
            });	
            
            //On Click
            $(".paging a").click(function() {	
                $active = $(this); //Activate the clicked paging
                //Reset Timer
                clearInterval(play); //Stop the rotation
                rotate(); //Trigger rotation immediately
                rotateSwitch(); // Resume rotation
                return false; //Prevent browser jump to link anchor
            });	
            
        });