// JavaScript Document

// review rollover javascript
var isSafari=false;
if ( navigator.vendor && navigator.vendor.indexOf("Apple")!=-1) isSafari=true;


var is_it_focused = "";



var is_it_over = "";



function changeClass(obj, class_name) {

    obj.className = class_name;

    return(obj.className);

}



function checkForCookie(cookie_name) {



    var searchString = cookie_name;

    

    var cookieValue = null;

    

    //alert("looking for " + cookie_name);

    

    var cookies = document.cookie.split(';');

    

    for(var i = 0; i < cookies.length; i++) {

        //alert(cookies[i].indexOf(searchString + '='));

        if(cookies[i].indexOf(searchString + '=') == 1){

            //alert("found the cookie");

            cookieValue = cookies[i].substring(cookies[i].indexOf('=')+1);

            

             //alert(cookieValue);

        }

    }

    

    

   

    

    return cookieValue;

}







function findPos(obj)

{

	var curleft = curtop = 0;

	if (obj.offsetParent) {

		curleft = obj.offsetLeft

		curtop = obj.offsetTop

		while (obj = obj.offsetParent) {

			curleft += obj.offsetLeft

			curtop += obj.offsetTop

		}

	}

	return [curleft,curtop];

}



function initReviewRollover () {

	var x = document.getElementsByTagName('a');

	

	for(var i = 0; i < x.length; i++){

		if(x[i].className != 'down-arrow reviews') continue;

		

		

			x[i].onmouseover = function() {

				

				var coors = findPos(this);

				var moveDiv = document.getElementById('review-rollover');

				moveDiv.style.visibility = "visible";

				var height = moveDiv.offsetHeight;

				var half_height = height/2;

				var too_far = document.body.scrollTop - (coors[1] - half_height);

				

			

				

				

				moveDiv.style.left = (coors[0] + 85) + 'px';

				

				if(too_far > 0) {

					moveDiv.style.top = (coors[1] + (too_far - half_height)) + 'px';

					//alert(moveDiv.style.top);

					var triangle = document.getElementById('triangle');
					
					if(isSafari) {
					    triangle.style.left = 13 + 'px';
					}

					var new_top = half_height - too_far;

					if(new_top < 9 )
						new_top = 9;

						

					//alert(new_top);	

					//alert(height);

						

					if(new_top >= (height - 42)) {

					    triangle.style.bottom = 12 + 'px';

					    //alert("here!");

					    //alert(triangle.style.bottom);

					} else {

					    triangle.style.top = new_top - 12 + 'px';

					}

					

				} else {

				    moveDiv.style.top = coors[1] - half_height + 'px';

				

				    var triangle = document.getElementById('triangle');

				

				    triangle.style.top = half_height - 12 + 'px';
				    
				    if(isSafari)
					    triangle.style.left = 13 + 'px';

				}

				



				

				

				moveDiv.onmouseover = function() {

				



					this.style.visibility = "visible";

					this.style.left = (coors[0] + 85) + 'px';

					

				if(too_far > 0) {

					this.style.top = (coors[1] + (too_far - half_height)) + 'px';

					var triangle = document.getElementById('triangle');

					var new_top = half_height - too_far - 12 + 'px';
					
					if(isSafari)
					    triangle.style.left = 13 + 'px';

					if(new_top < 9 )
						new_top = 9;

						

					//alert(new_top);	

					//alert(height);

						

					if(new_top > (height - 42)) {

					    triangle.style.bottom = 12 + 'px';

					} else {

					    triangle.style.top = new_top + 'px';

					}

					

				} else {

				    this.style.top = coors[1] - half_height + 'px';

				

				    var triangle = document.getElementById('triangle');

				    if(isSafari)
					    triangle.style.left = 13 + 'px';

				    triangle.style.top = half_height - 12 + 'px';

				}

						

				}

				

				moveDiv.onmouseout = function () {

					this.style.visibility = "hidden";

				}

				

		}

			x[i].onmouseout = function() {

				var coors = findPos(this);

				var moveDiv = document.getElementById('review-rollover');

				moveDiv.style.visibility = "hidden";

				moveDiv.style.left = coors[0] + 'px';

				moveDiv.style.top = coors[1] + 'px';

				

		}

		

	}

	



}



function initCheckStore () {

	var checkStoreMarkup = $('#check-store');
	
	if (! $(checkStoreMarkup).parent().get(0).tagName != 'body') {
		$(checkStoreMarkup).clone(true).appendTo('body');
		$(checkStoreMarkup).remove();
	}
	
	var x = document.getElementsByTagName('a');

	

	for(var i = 0; i < x.length; i++){

		if(x[i].className != 'down-arrow csa') continue;


		

			x[i].onmouseover = function() {

				

				var coors = findPos(this);

				var moveDiv = document.getElementById('check-store');

				//var too_far = document.body.scrollTop - (coors[1] - 265);

				

				moveDiv.style.visibility = "visible";

				

//				if (document.attachEvent) {

//					moveDiv.style.left = (coors[0] - 86) + 'px';

//					moveDiv.style.top = (coors[1] + 10) + 'px';

//					alert('hi');

//				} else {

					moveDiv.style.left = (coors[0] - 100) + 'px';

					moveDiv.style.top = (coors[1] + 12) + 'px';

//				}





                var inputs = moveDiv.getElementsByTagName('input');

                

                /*var cookieValue = checkForCookie('zip');



                



                if(cookieValue != null){

                    //alert("found cookie");

                   //alert(cookieValue);

				    inputs[0].value = cookieValue;

				    var backgroundDiv = document.getElementById('cs-bg2');

				    changeClass(backgroundDiv, "cs-bg"); 

				    

				} else {						



                    if(inputs[0].value == '') {

                        var backgroundDiv = document.getElementById('cs-bg2');

				        changeClass(backgroundDiv, "cs-bg-small"); 

				    }

				     

                }*/

					

                    inputs[0].onfocus = function() {

                        is_it_focused = "1";

					   

                        document.body.onclick = function () {

					    

                            if(is_it_over) {

                                //alert(moveDiv.style.visibilty);

                                moveDiv.style.visibility = "visible";

                                //alert("it's over");

                            }

                            else {

                                //alert("onclick visibility hidden set");

                                is_it_focused = "";

                                document.getElementById('check-store').style.visibility = "hidden";

                            }

                        }

                    }

				

				    

				

				

				moveDiv.onmouseover = function() {

				

				    is_it_over = "1";

				

					this.style.visibility = "visible";

//					if (document.attachEvent) {

//						this.style.left = (coors[0] - 86) + 'px';

//						this.style.top = (coors[1] + 10) + 'px';

//					} else {

						this.style.left = (coors[0] - 100) + 'px';

						this.style.top = (coors[1] + 12) + 'px';

//					}

				}

				

				moveDiv.onmouseout = function () {

				

				       is_it_over = "";

				

					   if(is_it_focused) {

					    //alert("mouse over visibile set");

					    this.style.visibility = "visible";

					   } 

					   else{

					    this.style.visibility = "hidden";

					   }

			    

					    

				}

				

		}

			x[i].onmouseout = function() {

				var moveDiv = document.getElementById('check-store');

				moveDiv.style.visibility = "hidden";	

		}

		

	}

	



}





addEventSimple(window,"load",initReviewRollover);

addEventSimple(window,"load",initCheckStore);



	function addEventSimple(obj,evt,fn) {

		if (obj.addEventListener)

			obj.addEventListener(evt,fn,false);

		else if (obj.attachEvent)

			obj.attachEvent('on'+evt,fn);

	}