var $j = jQuery.noConflict();
var companyName;

function normalSelectBehaviour(e, sender) {
    var key;
    if (window.event)
        key = window.event.keyCode; //IE
    else
        key = e.which; //firefox

    if (key == 13) {
        sender.blur();
        return false;
    }
}

$j(document).ready(function () {
	companyName = $j('.company-name').val();
	
	// Input - select elements, revert normal behaviour
    $j('select').keydown(function (e) {
        normalSelectBehaviour(e, this);
    });

	// Uniform - Form Replacement
	$j("select:not(.purchases select)").uniform();

	// Dropdown Menu
	var timeout = 500;
	var closetimer = 0;
	var ddmenuitem = 0;

	function jsddm_open() {
		jsddm_canceltimer();
		jsddm_close();
		ddmenuitem = $j(this).find('ul').css('visibility', 'visible');
	}

	function jsddm_close()
	{ if (ddmenuitem) ddmenuitem.css('visibility', 'hidden'); }

	function jsddm_timer()
	{ closetimer = window.setTimeout(jsddm_close, timeout); }

	function jsddm_canceltimer() {
		if (closetimer) {
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	}

	document.onclick = jsddm_close;


	$j('#jsddm > li').bind('mouseover', jsddm_open)
	$j('#jsddm > li').bind('mouseout', jsddm_timer)
	$j(".panel-show").click(function () {
		$j(this).parent().next(".profile-panel").slideToggle("slow");
		//alert(pnlid);
		//$j("#" + pnlid).css('background-color', '#000');
	});


	/* End Dropdown Menu */



	/* Handle search */

	$j(".search button").click(function (e) {
		var searchTerm = $j.trim($j(".search input").val());
		if (searchTerm.length > 0 && searchTerm != 'Search for...') {
			search();
		}
		return false;

	});

	$j(".search input").focus(function () {
		$j(this).keyup(function (e) {
			if (e.which == 13) {
				$j(".search input").blur();
				search();
				return false;
			}
		});
	});

	function search() {

		var searchTerm = $j.trim($j(".search input").val());
		if (searchTerm.length > 0 && searchTerm != 'Search for...') {
			var query = "/search.aspx?search=" + urlencode(searchTerm);
			window.location.replace(query);
		}
		else
			alert("Search is empty");
	}

	function urlencode(str) {
		return escape(str).replace(/\+/g, '%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
	}

	/* end search */


	/* Lightbox */

	$j(".lightbox-load").click(function () {
		var lightbox = $j(this).attr("id");
		$j('.' + lightbox + '.lightbox').show('slow');
		$j('.lightbox-load a').css('color', '#000');

		if (lightbox != "delete-collection" && lightbox != "create-collection-folder") {
			$j(this).addClass('lightbox-selected');
		}
		return false;
	});

    $j('.lbclose').live("click", function () {
		$j(this).offsetParent(".lightbox").hide("slow");
		$j('.lightbox-load').removeClass('lightbox-selected');
		return false;
	});

	/* end lightbox */


	/* Print */
	$j(".print").click(function () {
		window.print();
	});

	/* End print */

	/* Slider - using jQuery Tools, tabs and slideshow */
	/* $j("ul.home-promo-nav").tabs("div.panes > div", {
		effect: 'fade',
		fadeOutSpeed: 500,
		fadeInSpeed: 500,
		rotate: true,
		event: 'mouseover'
	}).slideshow({
		autoplay: true,
		interval: 6000
		}); */

	/* Slider - using Nivo Slider */
	if ($j("#slider").length > 0) {
		$j('#slider').nivoSlider({
			effect:'boxRandom,boxRain,boxRainReverse,boxRainGrow,boxRainGrowReverse',
			pauseTime:6000,
			pauseOnHover:true,
			boxCols:8,
			boxRows:5
		});
	}

	/* Tabs */
	
	/* Sidebar - Most... */
	$j("ul.sidebar-tabs-nav").tabs("div.sidebar-panes > div");
	
	// $j("#accordion").tabs("#accordion div.pane", { tabs: 'h2', effect: 'slide', initialIndex: null });

	// Tabs (My TT)
	$j(".mytt-module.reading-list ul").tabs(".mytt-module.reading-list div.panes > div", { effect: 'fade', event: 'mouseover' });
	$j(".mytt-module.recently-viewed ul").tabs(".mytt-module.recently-viewed div.panes > div", { effect: 'fade' });
	$j("ul.accountinfo-nav").tabs("div.accountpref > div:not(.validation-message)");

	// Scrollable (My TT)
	if ($j("div.scrollable").length > 0) {
		$j("div.scrollable").scrollable({
			prev: ".prevPage",
			next: ".nextPage",
			easing: "swing",
			speed: 750
		});
	}

	/* Company Dropdown change */
	$j(".company-name").blur(function () {
		if ($j(this).val() != companyName) {
			$j('.company-turnover option:eq(0)').attr("selected", "selected");
			$j('.company-turnover').prev().text($j(".company-turnover option:selected").text());
		}
	});

	

	if ($j(".help-icon").length > 0) {
		$j(".help-icon[title]").tooltip();
	}
	if ($j(".ttip").length > 0) {
		$j(".ttip[title]").tooltip();
	}

	/* Company dropdown */
	if ($j(".portrait a, .enlargeable a, a.enlargeable").length > 0) {
		$j(".portrait a, .enlargeable a, a.enlargeable").colorbox({
			initialWidth:600,
			initialHeight:400,
			opacity:"0.42",
			maxWidth:"90%",
			maxHeight:"90%"
		});
	}
	if ($j("#article-content .categories a").length > 0) {
		$j("#article-content .categories a").colorbox({
			initialWidth:300,
			initialHeight:200,
			opacity:"0.42",
			maxWidth:"300px",
			maxHeight:"84%",
			inline:true
		});
	}
});
