$(document).ready(function(){
	imgsInSearch();
	prepareLinks();
	prepareSlider();
});

// for images in search
function imgsInSearch(){
  if (!document.getElementById || !document.getElementsByTagName) {
    return;
  }
  if (!document.getElementById("searchitems")) {
    return;
  }
  // for all items with class searchurl, get the text of the href, parse it for ###.html,
  // get ### and inset correct product pic
  var theURL = $('.searchurl').text();
  var imgURLs = new Array(10);
  var linkURLs = new Array(10);
  // put in array based on "http://oriental-decor.com/"
  var pieces = theURL.split('http://oriental-decor.com/');
  for (var i=1;i<=10;i++) {
  	if (pieces[i]) {
      var end = pieces[i].indexOf(".html");
	  if(end > 0) {
        var start = pieces[i].lastIndexOf("/") + 1;
	    var prodNumber = pieces[i].substring(start,end);
	    imgURLs[i] = "http://oriental-decor.com/img/products/sss/"+prodNumber+".jpg";
	    linkURLs[i] = "http://oriental-decor.com/product/"+prodNumber+".html";
        var htmlInsert = '<a href="'+linkURLs[i]+'"><img style="float:left; margin:3px; padding:3px" src="'+imgURLs[i]+'"/></a>';
        $('.searchitem').eq(i-1).prepend(htmlInsert);
      }
    }
  }
}

var gotFile = false;
// for menu
function prepareLinks() {
  if (!document.getElementById || !document.getElementsByTagName) {
    return;
  }
  if (!document.getElementById("click-menu1x")) {
    return;
  }
  var url = "/children.html";
  gotFile = grabFile(url);
	$('dt:not(".group")').find('a:not("#child")').click(function(){
  		$('dd').slideUp("slow");
			$(this).parent().next().show(1000);
      return !gotFile;
 	});
}

// for slider
function prepareSlider(){
	if (!document.getElementById || !document.getElementsByTagName) {
    return;
  }
  if (!document.getElementById("trueContainer")) {
    return;
  }
  var url = "/slidegallery.html";
  gotFile = grabFile(url);
}

function grabFile(file) {
  var request = getHTTPObject();
  if (request) {
    request.onreadystatechange = function() {
      parseResponse(request);
    };
    request.open("GET", file, true);
    request.send(null);
    return true;
  } else {
    return false;
  }
}

function parseResponse(request) {
  if (request.readyState == 4) {
    if (request.status == 200 || request.status == 304) {
	  text = request.responseText;
	  pieces = text.split("***");
	  c = 0;
		theDL = $("dl");
	  for (var i=0;i<pieces.length;i++) {
		if (location.pathname.indexOf("wall-decor-paintings.php") < 1 &&
			location.pathname.indexOf("home-furniture.php") < 1  &&
			location.pathname.indexOf("asian-home-accents.php") < 1  &&
			location.pathname.indexOf("asian-lamps-and-lighting.php") < 1  &&
			location.pathname.indexOf("garden-decor.php") < 1  &&
			location.pathname.indexOf("asian-clothing-and-fashion.php") < 1 ) {
			//this one is for the slide gallery on the homepage
			if (pieces[i].indexOf("slidegallery") > 0) {
				$("#trueContainer").html(pieces[i]).show();
			}
			else if (pieces[i].indexOf("fancats") > 0) { $("#fancats").html(pieces[i]).hide();
				// this one will open on index page too
				if ($("dl").find("#fancats:first").html() != null) {
					if ($("dl").find("#fancats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#fancats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("umbcats") > 0) { $("#umbcats").html(pieces[i]).hide();
				if ($("dl").find("#umbcats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#umbcats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#umbcats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("handtablecats") > 0) { $("#handtablecats").html(pieces[i]).hide();
				if ($("dl").find("#handtablecats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#handtablecats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#handtablecats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("lampcats") > 0) { $("#lampcats").html(pieces[i]).hide();
				if ($("dl").find("#lampcats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#lampcats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#lampcats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("lanterncats") > 0) { $("#lanterncats").html(pieces[i]).hide();
				if ($("dl").find("#lanterncats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#lanterncats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#lanterncats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("shojicats") > 0) { $("#shojicats").html(pieces[i]).hide();
				if ($("dl").find("#shojicats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#shojicats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#shojicats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("beddingcats") > 0) { $("#beddingcats").html(pieces[i]).hide();
				if ($("dl").find("#beddingcats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#beddingcats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#beddingcats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("furniturecats") > 0) { $("#furniturecats").html(pieces[i]).hide();
				if ($("dl").find("#furniturecats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#furniturecats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#furniturecats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("patiocats") > 0) { $("#patiocats").html(pieces[i]).hide();
				if ($("dl").find("#patiocats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#patiocats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#patiocats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("vasecats") > 0) { $("#vasecats").html(pieces[i]).hide();
				if ($("dl").find("#vasecats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#vasecats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#vasecats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("statuecats") > 0) { $("#statuecats").html(pieces[i]).hide();
				if ($("dl").find("#statuecats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#statuecats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#statuecats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("paintingcats") > 0) { $("#paintingcats").html(pieces[i]).hide();
				if ($("dl").find("#paintingcats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#paintingcats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#paintingcats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("silkcats") > 0) { $("#silkcats").html(pieces[i]).hide();
				if ($("dl").find("#silkcats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#silkcats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#silkcats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("kitchencats") > 0) { $("#kitchencats").html(pieces[i]).hide();
				if ($("dl").find("#kitchencats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#kitchencats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#kitchencats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("scrollcats") > 0) { $("#scrollcats").html(pieces[i]).hide();
				if ($("dl").find("#scrollcats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#scrollcats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#scrollcats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("wallcats") > 0) { $("#wallcats").html(pieces[i]).hide();
				if ($("dl").find("#wallcats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#wallcats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#wallcats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("bonsaicats") > 0) { $("#bonsaicats").html(pieces[i]).hide();
				if ($("dl").find("#bonsaicats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#bonsaicats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#bonsaicats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("artificialcats") > 0) { $("#artificialcats").html(pieces[i]).hide();
				if ($("dl").find("#artificialcats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#artificialcats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#artificialcats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("giftcats") > 0) { $("#giftcats").html(pieces[i]).hide();
				if ($("dl").find("#giftcats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#giftcats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#giftcats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("homedecorcats") > 0) { $("#homedecorcats").html(pieces[i]).hide();
				if ($("dl").find("#homedecorcats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#homedecorcats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#homedecorcats:first").show(1000);
					}
				}
			}
			else if (pieces[i].indexOf("jewelrycats") > 0) { $("#jewelrycats").html(pieces[i]).hide();
				if ($("dl").find("#jewelrycats:first").html() != null) {
					if (location.pathname != "/" && $("dl").find("#jewelrycats:first").html().indexOf(location.pathname) > 0) {
						$("dl").find("#jewelrycats:first").show(1000);
					}
				}
			}
			c++;
		}
	  }
    }
  }
}

function getHTTPObject() {
  var xhr = false;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(e) {
        xhr = false;
      }
    }
  }
  return xhr;
}