// ************* DEFINE CUFON TEXT REPLACMENTS

// Main menu links
Cufon.replace('a.mi', { hover:true, fontFamily: 'FreightSans Medium' });

// make jQuery no conflic with other APIs
var $j = jQuery.noConflict();

var storyID = 0;
/*var activeLink = "link-1";
var activeNewsLink = "news-1";*/

// define on load font size (in %)
var fontSize = 100;

// extend jquery 
	$j.fn.wait = function(time, type) {
        time = time || 500;
        type = type || "fx";
        return this.queue(type, function() {
            var self = this;
            setTimeout(function() {
                $j(self).dequeue();
            }, time);
        });
    };


(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

// ************* DEFINE jQuery functions
$j(document).ready(function(){

	/* prelaod the images */
	jQuery.preLoadImages("/images/content-back.png","/images/submenu-back.png","/images/menu-back.png","/images/vancouver.png","/images/menu-contact.png","/images/menu-firm.png","/images/menu-home.png","/images/menu-lawyers.png","/images/menu-links.png","/images/menu-practice.png","/images/menu-news.png");


	// Define easing type
	//jQuery.easing.def = 'easeOutSine';
	
	// add printing
	$j("#print-page").click(function(event)
	{
		if($j("#content-overlay").length == 0) {
			$j("#lawyer-details").jqprint();
		}
		else {
			$j("#content-overlay").jqprint();
		}
	});
	
	// ************* DEFINE - Roll over events for any image tagged with .over
	$j('img.over').each(function() 
	{
    	var t = $j(this);
    	var src = t.attr('src'); // initial src
    	var path = src.substring(0,src.lastIndexOf('/')); // path to file
    	var filename = src.substring(src.lastIndexOf('/'), src.lastIndexOf('.')); // let's get file name without extension
    	
    	t.hover(function(){
        	$j(this).attr('src', path + filename + '-over.' + /[^.]+$/.exec(src)); //last part is for extension 
        	
        	//alert(path+filename+ '-over.' + /[^.]+$/.exec(src));
        	
    	}, function(){
        	$j(this).attr('src', path + filename + '.' + /[^.]+$/.exec(src)); //removing '-over' from the name
        	
        	//alert(path + filname + '.' + /[^.]+$/.exec(src));
    	});
	});
	
	/* NEW - bold names when roll over images */
	var thumb_person;
	$j("div.lThumb").hover(
		
		function () {
			// mouse over
			thumb_person = $j(this).find("a").attr("class");
			$j('a.' + thumb_person).addClass("embolden");
      	}, 
      	function () {
			// mouse out
			$j('a.' + thumb_person).removeClass("embolden");
      	}
	);
	
	/* NEW - outline image when roll over names */
	var link_person;
	$j("li.item-lawyer").hover(
		
		function () {
			// mouse over
			link_person = $j(this).find("a").attr("class");
			$j("#lawyer-thumbs").find('a.' + link_person).addClass('border');
      	}, 
      	function () {
			// mouse out
			$j("#lawyer-thumbs").find('a.' + link_person).removeClass('border');
      	}
	);
	
	
	
	// ************* DEFINE - main menu events
	$j("li.menu-item").hover(
		
		function () {
			/* mouse over */
			$j(this).addClass("menu-item-over");
      	}, 
      	function () {
			/* mouse out */
        	var isActive = $j(this).attr("id");
			if(isActive != "mi-active") $j(this).removeClass("menu-item-over");
      	}

	);
	
	$j("li.menu-item").mouseenter(function(event)
	{
		var sub = $j(this).find("ul.dumoulin-submenu");
		sub.stop(true, true).slideDown("normal");
		
		//$j(this).addClass("menu-item-over");
		
	});
	
	
	$j("li.menu-item").mouseleave(function(event)
	{
		var sub = $j(this).find("ul.dumoulin-submenu");
		sub.stop(true, true).slideUp(100);
		
		//make sure to hide the thrid level lawyers menu
		$j("#partners-sub").hide();
		$j("#associates-sub").hide();
		
		//$j(this).removeClass("menu-item-over");
		
	});
	
	
	/* Third level menu */
	$j("#associates-link").mouseenter(function(event)
	{
		$j("#partners-sub").stop(true, true).hide();
		$j("#associates-sub").stop(true, true).slideDown("normal");	
	});
	
	$j("#partners-link").mouseenter(function(event)
	{
		$j("#associates-sub").stop(true, true).hide();
		$j("#partners-sub").stop(true, true).slideDown("normal");	
	});
	
	
	/* internal links for our firm page 
	$j("a.internal").click(function(event)
	{
		if(activeLink != '') {
			$j("#" + activeLink).css('font-weight','normal');
			$j('#content-' + activeLink).hide();
		}
		
		activeLink = $j(this).attr("id");
		
		$j(this).css('font-weight','bold');
		$j('#content-' +  activeLink).fadeIn('normal');
		
		//$j(this).addClass("menu-item-over");
		
	});*/
	
	
	/* internal links for news page 
	$j("a.internal-news").click(function(event)
	{
		if(activeNewsLink != '') {
			$j("#" + activeNewsLink).css('font-weight','normal');
			$j('#full-' + activeNewsLink).hide();
		}
		
		activeNewsLink = $j(this).attr("id");
		
		$j(this).css('font-weight','bold');
		$j('#full-' +  activeNewsLink).fadeIn('normal');
		
		//$j(this).addClass("menu-item-over");
		
	});*/
	
	// ************* DEFINE - exapnd links
	$j("li.expand-link").hover(
		
		function () {
			/* mouse over */
			var expand = $j(this).find("ul.expand");
			$j(expand).show();
			
			$j(this).find("span").text("[-]");
			
			
      	}, 
      	function () {
			/* mouse out */
        	var expand = $j(this).find("ul.expand");
			$j(expand).hide();
			
			$j(this).find("span").text("[+]");
      	}
	);
	
	// ************* DEFINE - load homepage news
	$j("div.news-item").each( function(index) 
    {
    	var file = $j(this).attr('title');
	
		$j(this).load("/news/"+ file, function(response, status, xhr) {
			if (status == "error") {
    			var msg = "Sorry but there was an error: ";
    			$j("#error").html(msg + xhr.status + " " + xhr.statusText);
  			} 
  			else {
  				var str = response;
				var pbr = str.indexOf("<!--");
				var shorttag = str.substring(0,pbr) + ' [...]<br><a href="/news/#' + file + '">More</a></p>';
				$j(this).html(shorttag);
				$j(this).show();
			}
		});
	});
	
	// ************* DEFINE - load news page
	$j("#full-news-1").load("/news/news-1.html", function(response, status, xhr) {
		if (status == "error") {
    		var msg = "Sorry but there was an error: ";
    		$j("#error").html(msg + xhr.status + " " + xhr.statusText);
  		} 
  		else {
			$j('#news-1').html(linkTitle(response));
		}
	});

	$j("#full-news-2").load("/news/news-2.html", function(response, status, xhr) {
  		if (status == "error") {
    		var msg = "Sorry but there was an error: ";
    		$j("#error").html(msg + xhr.status + " " + xhr.statusText);
  		} 
		else {
			$j('#news-2').html(linkTitle(response));
		}
	});

	$j("#full-news-3").load("/news/news-3.html", function(response, status, xhr) {
  		if (status == "error") {
    		var msg = "Sorry but there was an error: ";
    		$j("#error").html(msg + xhr.status + " " + xhr.statusText);
  		} 
  		else {
			$j('#news-3').html(linkTitle(response));
		}
	});
	
	
	/*$j("#partners-sub").mouseleave(function(event)
	{
		$j(this).stop(true, true).slideUp("normal");
	});*/
	

	/* Legacy code - (can be deleted)
	
	var sub = $j(this).find("ul.hivnet-submenu");
    sub.slideUp("normal");
        
    var browser=navigator.appName;
	if(browser.indexOf("Microsoft") == -1) {}
	
	
	$j("div.project").mouseenter(function(event)
	{
		$j(this).addClass("over-border");

	});
	
	$j("div.project").click(function(event)
	{
		var desc = $j(this).find("p.project-desc");
		desc.slideDown("normal");
	});
	
	$j("div.project").mouseleave(function(event)
	{
		var desc = $j(this).find("p.project-desc");
		desc.slideUp("normal");
		
		$j(this).removeClass("over-border");

	});
	
	
	$j("#creative-box").css("opacity",0);
	

	$j("#creative-box").wait().animate({opacity: 1}, 1600,function(){
  		$j("#creative-box").animate({ height: 500 + 'px'}, 1500 );
	});*/
	
});

function linkTitle(response) {
	var str = response;
	var s = str.indexOf("<h3>");
	var e = str.indexOf("</h3>");
	var linktag = str.substring(s+4,e);
	return linktag;
}


/* function - loops headline on the homepage */
var wait = setInterval(showHeadline,6000);

function showHeadline() 
{
	// get object to fade out
	var f_out = "#headline-" + storyID;
	
	// get the id of object to fade in
	if(storyID >= 3) storyID = 0;
	else storyID ++;
	
	// get object to fade in
	var f_in = "#headline-" + storyID;

	$j(f_out).fadeOut(1000,function() {
		// Animation complete now fade in new story
		$j(f_in).fadeIn("fast");
	});
}

function initHeadline()
{
	// ************* DEFINE - home page fadein headline and headline-back
	/*$j("#headline-back").wait(1000).slideDown(1000,function() {
		// Animation complete now fade in new story
		$j("#headline-1").find('p').wait(500).fadeIn("fast");
	});*/
	
	$j("#headline-0").find('p').wait(1500).fadeIn("fast");
}


/* function - sets the active menu section and change link style */
function setActiveMenu(title)
{
	var t = title.toLowerCase();
	var id = "#mi-";
	
	//if(t.indexOf('home') != -1) id += "home";
	if(t.indexOf('the firm') != -1) id += "firm";
	if(t.indexOf('meet our lawyers') != -1) id += "lawyers";
	if(t.indexOf('our practice') != -1) id += "practice";
	if(t.indexOf('useful links') != -1) id += "links";
	if(t.indexOf('news') != -1) id += "news";
	if(t.indexOf('contact') != -1) id += "contact";
	
	if(id != "#mi-")
	{
		var obj = $j(id).find('a.menu-link');
		obj.addClass("menu-link-active");
		obj.removeClass("menu-link");
	}
	
	/* special case for resizing contact page */
	if(t.indexOf('contact') != -1)
	{
		$j("#content-overlay").css('width','910px');
	}
	
	/* special case for meet our lawyers 
	if(t.indexOf('meet our lawyers') != -1)
	{
		$j("div.lThumb").fadeTo(100, 0.5);
	}*/
}
	

/* Preload images and rollover functions */

var imgPreloads = ["/images/submenu-back.png"];
var imgObj = new Image();

for(var i=0; i<imgPreloads; i++)
{
	imgObj.src = imgPreloads[i];
}


function swapImg(obj,img) 
{
	obj.src = img;
}
	
	
	
	
	
	
	
	
	
	