var help = false;

$().ready(function(){

	// HELP functions //////////////////////////////////////////////////////////
	$(document).find("#help_activator").click(function() {
		if (help == false) {
			help = true;
			$(this).addClass("helpme");
			$(document).find(".help").addClass("help_highlight");
			
		} else {
			help = false;
			$(this).removeClass("helpme");
			$(document).find(".help").removeClass("help_highlight");
		}
	});
	$(document).find(".help").hover(
		function() {
			if (help == true) {
				$(this).css("cursor","help");
				$(this).attr("title","Click here to get help.");
			}
		},
		function() {
			$(this).css("cursor","default");
			$(this).attr("title","");
		}
	);
	
		
	// for pre function from codinghelpers.inc //////////////////////////////////////////////////////////
	$(document).find(".preDiv").click(
			function(){
				$(this).find(".prePre").toggle(); 
			}
	);




	// Mouseover for Products: Red text while hover over image
	$(".product").mouseover(function() {
		$pText = $(this).children(".productText");
		$pText.css('color', '#8d3736');
	})
	
	$(".product").mouseout(function() {
		$pText = $(this).children(".productText");
		$pText.css('color', '#818181');
	})
	
	// Mouseover for Diamond Shape Select
	$(".shapeImg").mouseover(function() {
		$shapeText = $(this).prev();
		$shapeText.css('visibility', 'visible');
	})
	
	$(".shapeImg").mouseout(function() {		
		$shapeText = $(this).prev();
		$shapeText.css('visibility', 'hidden');
	})
	
	// Mouseover for Slider Handles
	$(".ui-slider-handle img").mouseover(function() {
		$(this).attr('src','/img/sliderHandleActive.png');
		
	})
	$(".ui-slider-handle img").mouseout(function() {
		$(this).attr('src','/img/sliderHandle.png');
		
	})
	
	// LiveChat Hover
	$('#id_livechat_btn').mouseover(function() {
		$("#id_livechat_hover").css({"background":"transparent url(../images/checkout/live_chat_hover.png) 0px 0px no-repeat",
									"visibility":"visible"});
		
	});
	$('#id_livechat_hover_cl').click(function() {
		$("#id_livechat_hover").css({"background":"none",
									"visibility":"hidden"});
		
	});
	
	// Mouseover for Shopping basket
	$('#shoppingbasketHolder').mouseover(function() {
		$("#shoppingbasketHolder").css("background","transparent url(../img/basket_bg.png) 0px 0px repeat-y");
		$("#shoppingbasketContent").css("display","block");
		$(".shoppingbasketFooter").css("display","block");
	});
	$('#shoppingbasketHolder').mouseout(function() {
		$("#shoppingbasketContent").css("display","none");
		$(".shoppingbasketFooter").css("display","none");
		$("#shoppingbasketHolder").css("background","none");
	});
	
	$("#id_topnavi li a").mouseover(function() {
		if($("#id_subNavi #"+$(this).attr('id')+"_sub").length > 0) {
			if($("#id_subNavi").css("display")=="block") {
				$("#id_subNavi ul").css("display","none");
				$("#id_subNavi #"+$(this).attr('id')+"_sub").css("display","block");
			} else {
				$("#id_subNavi #"+$(this).attr('id')+"_sub").css("display","block");
				$("#id_subNavi").fadeIn("fast");
			}
		} else {
			if($("#id_subNavi").css("display")=="block") {
				$("#id_subNavi").fadeOut("fast",function() {
					$("#id_subNavi ul").css("display","none");
				});
			}
		}
	});
	
	
	
	
	/*$("#id_subNavi").mouseleave(function() {
		$("#id_subNavi").fadeOut("fast",function() {
			$("#id_subNavi ul").css("display","none");
		});
	});*/
	
	var hover_status = false;
	
	/*$("#id_subNavi").mouseout(function() {
		//console.debug("HOVER STATUS>>> "+$("#id_subNavi ul").data("hover"));
		console.debug("HOVER STATUS>>> "+hover_status);
		if(!hover_status) {
			console.debug("FADEOUT!!!");
			$("#id_subNavi").fadeOut("fast",function() {
				$("#id_subNavi ul").css("display","none");
			});
		}
	});*/
	
	function closeSubNavi() {
		$("#id_subNavi").fadeOut("fast",function() {
			$("#id_subNavi ul").css("display","none");
		});
	}
	
	function handleTopnaviEvents (e) {
		if (!e) var e = window.event;
		var tg = (window.event) ? e.srcElement : e.target;
		if ((tg.nodeName != 'UL')&&(tg.nodeName != 'A')) return;
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		//not the subnavi...
		if((reltg.nodeName=="UL")||(reltg.nodeName=="A")||(reltg.getAttribute("id")=="id_subNavi")) {
			return;
		}
		while (reltg != tg && reltg.nodeName != 'BODY')
			reltg= reltg.parentNode
		if (reltg== tg) return;
		// Mouseout took place when mouse actually left layer
		// Handle event
		closeSubNavi();
	}
	
	function handleSubnaviEvents(e) {
		if (!e) var e = window.event;
		var tg = (window.event) ? e.srcElement : e.target;
		if (tg.nodeName != 'DIV') return;
		var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
		//not the topnavi...
		if((reltg.nodeName=="UL")||(reltg.nodeName=="A")) {
			return;
		}
		while (reltg != tg && reltg.nodeName != 'BODY')
			reltg= reltg.parentNode
		if (reltg== tg) return;
		// Mouseout took place when mouse actually left layer
		// Handle event
		closeSubNavi();
	}
	
	
	//handles the subnavi mouseout events
	var subnav = document.getElementById("id_subNavi");
	subnav.onmouseout = handleSubnaviEvents;
	
	//and we have to handle the topnavi...
	var topnav = document.getElementById("id_topnavi");
	topnav.onmouseout = handleTopnaviEvents;
	
	//get ready for some of the dirtiest hacks ever!
	/*$("#id_subNavi #subnavi_limiter_bottom").mouseover(function() {
		$("#id_subNavi").fadeOut("fast",function() {
			$("#id_subNavi ul").css("display","none");
		});
	});
	
	$("#id_subNavi #subnavi_limiter_left").mouseover(function() {
		$("#id_subNavi").fadeOut("fast",function() {
			$("#id_subNavi ul").css("display","none");
		});
	});
	
	$("#id_subNavi #subnavi_limiter_right").mouseover(function() {
		$("#id_subNavi").fadeOut("fast",function() {
			$("#id_subNavi ul").css("display","none");
		});
	});*/
	
	
	function markSubNaviLinks() {
		var act_url = window.location;
		$("#id_subNavi a").each(function() {
			if($(this).attr("href")==act_url) {
				$(this).addClass("active");
			}
		});
	}
	markSubNaviLinks();
	
	
	
	
	
	//make an appointment link actions
	$("#make_an_appointment_btn").click(function() {
		var act_body_height = $("body").height();
		$("#blackdown_screen").css("height",act_body_height+"px");
		$("#blackdown_screen").fadeIn("fast",function() {
			$("#appointment_layer").fadeIn("fast");
		});
		return false;
	});
	
	$("#appointment_layer #ap_layer_close_btn").click(function() {
		$("#appointment_layer").fadeOut("fast",function() {
			$("#blackdown_screen").fadeOut("fast");
		});
	});
	
	$("#blackdown_screen").click(function() {
		$("#appointment_layer").fadeOut("fast",function() {
			$("#blackdown_screen").fadeOut("fast");
		});
	});
	
	//click to send appointment form
	$("#appointment_layer form").submit(function() {
		//check the fields
		var check = true;
		$(this).find("input,textarea").each(function() {
			if($(this).val()=="") {
				check = false;
				$(this).css("border","1px solid #A32F32");
			}
		});
		
		if(check==true) {
			var form_content = $(this).serializeArray();
			$.post(ajaxpath+'url/sendAppointmentForm',form_content,function(data) {
				if(data.sendstatus==true) {
					$("#appointment_layer").fadeOut("fast");
					$("#sendstatus_layer").text("Your appointment request has been sent! Please check your email inbox for messages.");
					$("#sendstatus_layer").fadeIn("fast");
					setTimeout(function() { $("#sendstatus_layer").fadeOut("fast",function() { $("#blackdown_screen").fadeOut("fast") }); },5000);
				} else {
					$("#appointment_layer").fadeOut("fast");
					$("#sendstatus_layer").text("There has been a problem with your request. Please try again or call customer service.");
					$("#sendstatus_layer").fadeIn("fast");
					setTimeout(function() { $("#sendstatus_layer").fadeOut("fast",function() { $("#blackdown_screen").fadeOut("fast") }); },5000);
				}
			},"json");
			return false;
		}
	});
});

	
	// Cookie functions
	function setCookie(key,value,validTime) {
		now = new Date();
		expDate = new Date(now.getTime()+validTime*86400000);
		document.cookie = key + '=' + value + '; expires=' + expDate.toGMTString() + ';';	
	}
	
	
	function explode (delimiter, string, limit) {
    // http://kevin.vanzonneveld.net
    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
    var emptyArray = {
        0: ''
    };

    // third argument is not required
    if (arguments.length < 2 || typeof arguments[0] == 'undefined' || typeof arguments[1] == 'undefined') {
        return null;
    }

    if (delimiter === '' || delimiter === false || delimiter === null) {
        return false;
    }

    if (typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object') {
        return emptyArray;
    }

    if (delimiter === true) {
        delimiter = '1';
    }

    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}

function array_pop (inputArr) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // +   input by: Theriault
    // %        note 1: While IE (and other browsers) support iterating an object's
    // %        note 1: own properties in order, if one attempts to add back properties
    // %        note 1: in IE, they may end up in their former position due to their position
    // %        note 1: being retained. So use of this function with "associative arrays"
    // %        note 1: (objects) may lead to unexpected behavior in an IE environment if
    // %        note 1: you add back properties with the same keys that you removed
    // *     example 1: array_pop([0,1,2]);
    // *     returns 1: 2
    // *     example 2: data = {firstName: 'Kevin', surName: 'van Zonneveld'};
    // *     example 2: lastElem = array_pop(data);
    // *     returns 2: 'van Zonneveld'
    // *     results 2: data == {firstName: 'Kevin'}
    var key = '',
        lastKey = '';

    if (inputArr.hasOwnProperty('length')) {
        // Indexed
        if (!inputArr.length) {
            // Done popping, are we?
            return null;
        }
        return inputArr.pop();
    } else {
        // Associative
        for (key in inputArr) {
            if (inputArr.hasOwnProperty(key)) {
                lastKey = key;
            }
        }
        if (lastKey) {
            var tmp = inputArr[lastKey];
            delete(inputArr[lastKey]);
            return tmp;
        } else {
            return null;
        }
    }
}

function implode (glue, pieces) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Waldo Malqui Silva
    // +   improved by: Itsacon (http://www.itsacon.net/)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: implode(' ', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'Kevin van Zonneveld'
    // *     example 2: implode(' ', {first:'Kevin', last: 'van Zonneveld'});
    // *     returns 2: 'Kevin van Zonneveld'
    var i = '',
        retVal = '',
        tGlue = '';
    if (arguments.length === 1) {
        pieces = glue;
        glue = '';
    }
    if (typeof(pieces) === 'object') {
        if (Object.prototype.toString.call(pieces) === '[object Array]') {
            return pieces.join(glue);
        } else {
            for (i in pieces) {
                retVal += tGlue + pieces[i];
                tGlue = glue;
            }
            return retVal;
        }
    } else {
        return pieces;
    }
}

function end (arr) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Legaev Andrey
    // +    revised by: J A R
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   restored by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    revised by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: Uses global: php_js to store the array pointer
    // *     example 1: end({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});
    // *     returns 1: 'Zonneveld'
    // *     example 2: end(['Kevin', 'van', 'Zonneveld']);
    // *     returns 2: 'Zonneveld'
    // BEGIN REDUNDANT
    this.php_js = this.php_js || {};
    this.php_js.pointers = this.php_js.pointers || [];
    var indexOf = function (value) {
        for (var i = 0, length = this.length; i < length; i++) {
            if (this[i] === value) {
                return i;
            }
        }
        return -1;
    };
    // END REDUNDANT
    var pointers = this.php_js.pointers;
    if (!pointers.indexOf) {
        pointers.indexOf = indexOf;
    }
    if (pointers.indexOf(arr) === -1) {
        pointers.push(arr, 0);
    }
    var arrpos = pointers.indexOf(arr);
    if (Object.prototype.toString.call(arr) !== '[object Array]') {
        var ct = 0;
        for (var k in arr) {
            ct++;
            var val = arr[k];
        }
        if (ct === 0) {
            return false; // Empty
        }
        pointers[arrpos + 1] = ct - 1;
        return val;
    }
    if (arr.length === 0) {
        return false;
    }
    pointers[arrpos + 1] = arr.length - 1;
    return arr[pointers[arrpos + 1]];
}

function array_push (inputArr) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: Note also that IE retains information about property position even
    // %        note 1: after being supposedly deleted, so if you delete properties and then
    // %        note 1: add back properties with the same keys (including numeric) that had
    // %        note 1: been deleted, the order will be as before; thus, this function is not
    // %        note 1: really recommended with associative arrays (objects) in IE environments
    // *     example 1: array_push(['kevin','van'], 'zonneveld');
    // *     returns 1: 3
    var i = 0,
        pr = '',
        argv = arguments,
        argc = argv.length,
        allDigits = /^\d$/,
        size = 0,
        highestIdx = 0,
        len = 0;
    if (inputArr.hasOwnProperty('length')) {
        for (i = 1; i < argc; i++) {
            inputArr[inputArr.length] = argv[i];
        }
        return inputArr.length;
    }

    // Associative (object)
    for (pr in inputArr) {
        if (inputArr.hasOwnProperty(pr)) {
            ++len;
            if (pr.search(allDigits) !== -1) {
                size = parseInt(pr, 10);
                highestIdx = size > highestIdx ? size : highestIdx;
            }
        }
    }
    for (i = 1; i < argc; i++) {
        inputArr[++highestIdx] = argv[i];
    }
    return len + i - 1;
}

