function parseUri(sourceUri){
    var uriPartNames = ["source","protocol","authority","domain","port","path","directoryPath","fileName","query","anchor"];
    var uriParts = new RegExp("^(?:([^:/?#.]+):)?(?://)?(([^:/?#]*)(?::(\\d*))?)?((/(?:[^?#](?![^?#/]*\\.[^?#/.]+(?:[\\?#]|$)))*/?)?([^?#/]*))?(?:\\?([^#]*))?(?:#(.*))?").exec(sourceUri);
    var uri = {};
    
    for(var i = 0; i < 10; i++){
        uri[uriPartNames[i]] = (uriParts[i] ? uriParts[i] : "");
    }
    
    // Always end directoryPath with a trailing backslash if a path was present in the source URI
    // Note that a trailing backslash is NOT automatically inserted within or appended to the "path" key
    if(uri.directoryPath.length > 0){
        uri.directoryPath = uri.directoryPath.replace(/\/?$/, "/");
    }
    
    return uri;
}


$(document).ready(function(){
	$("#leftmenu").accordion({
		header: ".ui-accordion-link",
		clearStyle: true,
		autoheight: false,
		event: "mouseover"
	});
	
	var uri = document.URL; // expand active menu
	var arr = parseUri(uri);
	var splitteduri = arr["directoryPath"].split("/");
	if (splitteduri[2] == "our_products") {
		$('#leftmenu').activate(1);
	} else if (splitteduri[2] == "why_inmart") {
		$('#leftmenu').activate(2);
	} else {
		$('#leftmenu').activate(0);
	}
	
	/*
	if (splitteduri[2] == "about_us") { // display highlighted image on active menu
		$('#aboutus_a').removeClass('rollover');
		$('#aboutus_img').attr('src','/img/home_14_o.jpg');
	} else if (splitteduri[2] == "our_customers") {
		$('#ourcust_a').removeClass('rollover');
		$('#ourcust_img').attr('src','/img/home_16_o.jpg');
	} else if (splitteduri[2] == "partners_corner") {
		$('#partners_a').removeClass('rollover');
		$('#partners_img').attr('src','/img/home_20_o.jpg');
	} else if (splitteduri[1] == "contact_us") {
		$('#contact_a').removeClass('rollover');
		$('#contact_img').attr('src','/img/home_22_o.jpg');
	}
	*/

	// image chnage on active menu item
	$('a.rollover').each(
		function() {
			cururl = $(this).attr("href");
			hreflength = arr["directoryPath"].length;
			tmphref = arr["directoryPath"].substr(0, hreflength-1);
			normalimg = $(this).find("img").attr("src");
			name = normalimg.length;
			hover = normalimg.substr(0, name-4);
			hover = hover+'_o.jpg';
			if (cururl == tmphref) {
				$(this).find("img").attr("src",hover);
				$(this).removeClass('rollover');
				if (cururl.match("technology")) { $('#imageswap1').attr("src","/img/technology_28.jpg"); }
				else if (cururl.match("project_flow")) { $('#imageswap1').attr("src","/img/project_28.jpg"); }
				else if (cururl.match("online_marketing")) { $('#imageswap1').attr("src","/img/online_28.jpg"); }
				else if (cururl.match("market_information")) { $('#imageswap1').attr("src","/img/market_28.jpg"); }
				else if (cururl.match("get_a_quote")) { $('#imageswap1').attr("src","/img/quote_28.jpg"); }
				else if (cururl.match("faq")) { $('#imageswap1').attr("src","/img/faq_28.jpg"); }
				else if (cururl.match("performace_pledge")) { $('#imageswap1').attr("src","/img/performance_28.jpg"); }
				else $('#imageswap1').attr("src","/img/other_28.jpg");
				
			} else if (splitteduri[2] == "our_services") {
				$("#menucat1").css("background-image","url(/img/home_31_o.jpg)");
				$('#imageswap1').attr("src","/img/our_services_28.jpg");
			} else if (splitteduri[2] == "our_products") {
				$("#menucat2").css("background-image","url(/img/home_32_o.jpg)");
				$('#imageswap1').attr("src","/img/our_product_28.jpg");
			} else if (splitteduri[2] == "why_inmart") {
				$("#menucat3").css("background-image","url(/img/home_33_o.jpg)");
				$('#imageswap1').attr("src","/img/why_28.jpg");
			} else if (cururl.match("newsroom") && arr["directoryPath"].match("newsroom")) {
				$(this).find("img").attr("src",hover);
				$(this).removeClass('rollover');
			}
		}
	);
	
	$('#testimonial').cycle({
		fx: 'scrollUp', 
		timeout: 6000, 
		delay:  -6000
	});
	
	$(".rollover").hover(
		function() {
			normal = $(this).find("img").attr("src");
			name = normal.length;
			hover = normal.substr(0, name-4);
			hover = hover+'_o.jpg';
			$(this).find("img").attr({ src: hover});
		},
			function() {
			$(this).find("img").attr({ src: normal});
		}
	);

	$(".rollover").find("img").each(function(i) {
		temp = this.src;
		tmpname = temp.length;
		pre = temp.substr(0, tmpname-4);
		pre = pre+'_o.jpg';
		preload_image_object = new Image();
		preload_image_object.src = pre;
	});
	
});