$(document).ready(function() {
	

    if ($("#homeimages").length > 0){
        $('#homeimages').cycle({
            fx:     'fade',
            speed:  500,
            timeout: 7000,
            prev: "#homeimgNav .prev",
            next: "#homeimgNav .next",
            cleartypeNoBg: true
        });
    }

    if ($("#banners").length > 0){
        $('#banners').cycle({
            fx:     'fade',
            speed:  500,
            timeout: 5000,
            cleartypeNoBg: true
        });
    }

    if ($("#workWithRotator").length > 0){
        $('#workWithRotator').cycle({
            fx:     'fade',
            speed:  500,
            timeout: 5000,
            cleartypeNoBg: true
        });
    }
    
    if ($(".quoteRotator").length > 0){
        $('.quoteRotator').cycle({
            fx:     'fade',
            speed:  500,
            timeout: 5000,
            cleartypeNoBg: true
        });
    }
    
    if ($("#quoteRotatorHome").length > 0){
        $('#quoteRotatorHome').cycle({
            fx:     'fade',
            speed:  500,
            timeout: 5000,
            cleartypeNoBg: true
        });
    }



    $('.subnav').click(function() {
       $(".main_body_area_right").css({"display":"none"});
       $(".subnav").removeClass("active");
       $(this).addClass("active");
    });

    $("a.fancy").fancybox();
    
    /*if(jQuery.support.leadingWhitespace){
    
    Cufon.replace('#cs-subnav a', { fontFamily: 'Gotham Bold', hover: true });
	Cufon.replace('.img-intro-text, .menu', { fontFamily: 'Gotham Book' });
	Cufon.replace('.menu a, .phone, .textholdfeatured, .title, .title_blue, .tools-heading h1, .main_left_area h2, #main_title h1, .gothbold', { fontFamily: 'Gotham Bold' });
	Cufon.replace('.head, .book', { fontFamily: 'Gotham Book' });
	
	Cufon.replace('.main_left_text3 p.quote', { fontFamily: 'Gotham Book' });
	}
	Cufon.replace('.navigation', { fontFamily: 'Gotham Bold', hover: true });
	*/
	if(!jQuery.support.cssFloat){
	
		$('#middle_tools_body_area').css("marginTop", 135);
	
	}
	

});


function show_hide_content(obj_id){
    $("#content"+obj_id).css({"display":"block"});
}

function tools_load_skin(image_src){

     $("#wrap_middle_buttons_tools div a").removeClass("active");

     $(".main_left_area").removeClass("displayed");
     $(".main_left_area").addClass("hidden");

     $(".tool-group-wrapper").removeClass("displayed");
     $(".tool-group-wrapper").addClass("hidden");

    if(image_src.indexOf("/green") !== -1){
        $("#heading-circle").attr('src','images/tools-circle-green.png');
        $("#button_tools3 a").addClass("active");
        $("#main-green-title").addClass("displayed");
        $("#group-green").addClass("displayed");
    }

    if(image_src.indexOf("/orange") !== -1){
        $("#heading-circle").attr('src','images/tools-circle-orange.png');
        $("#button_tools1 a").addClass("active");
        $("#main-orange-title").addClass("displayed");
        $("#group-orange").addClass("displayed");
    }

    if(image_src.indexOf("/yellow") !== -1){
        $("#heading-circle").attr('src','images/tools-circle-yellow.png');
        $("#button_tools2 a").addClass("active");
        $("#main-yellow-title").addClass("displayed");
        $("#group-yellow").addClass("displayed");
    }
    
}

function set_skin(colour){
    
    if(colour == "green"){
        $("#heading-circle").attr('src','images/tools-circle-green.png');
    } else if(colour == "orange"){
        $("#heading-circle").attr('src','images/tools-circle-orange.png');
    } else if(colour == "yellow"){
        $("#heading-circle").attr('src','images/tools-circle-yellow.png');
    }
    
}

function gotopage(url){
    if(url != 0){
        document.location.href = url;
    }
}

/* Drop Down Menu */
function dropDownMenus() {

	var cmTrigger = jQuery('.dropDownMenu .dropDownMenuLabel');
	var cmPanel = cmTrigger.next();
	
	jQuery(document).click(function() {
		cmPanel.parent().removeClass('dropDownMenuOpen').addClass('dropDownMenu');
	});
	
	jQuery(cmPanel).click(function(e) {
		e.stopPropagation();
	});

	cmTrigger.click(function(e) {

		e.stopPropagation();

		var cmClicked = jQuery(this);
		var closedClass = cmClicked.parent().attr('class');
		
		//hide panels that are currently showing		
		cmPanel.parent().removeClass('dropDownMenuOpen').addClass('dropDownMenu');

		if (cmClicked.parent().attr('class') == closedClass) {

			cmClicked.parent().removeClass('dropDownMenu').addClass('dropDownMenuOpen');

		} else {
			cmClicked.parent().removeClass('dropDownMenuOpen').addClass('dropDownMenu');
		}

	});

}
/* Drop Down Menu End */

/* USEFUL */

function activateURLs(stringToConvert){
	var rx = new RegExp(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig);
	return stringToConvert.replace(rx, "<a href='$1' target='_blank'>$1</a>");
}


/* TWITTER API */

//	function getTweets
//	returns string containing <tweetCount> tweets from @<screenname> in a specified <outputTemplate>
//	outputTemplate:			default benaviour, output all tweets in a string	
//					p 		wraps each tweet in <p> & </p> tag. URLs are output as hyperlinks
//					divP 	wraps each tweet in <div> & </p> tag. URLs are output as hyperlinks
//					divSpan wraps each tweet in <div> & </span> tag. URLs are output as hyperlinks
//					div 	wraps each tweet in <div> & </div> tag. URLs are output as hyperlinks
//					li		wraps each tweet in <li> & <li> tags. URLs are output as hyperlinks
//					ul		as li, but outputs list items within unordered list tags
//					ol		as li, but outputs list items within ordered list tags
//	outputLocation	ID of the container where HTML should be output
//	uses Twitter's API: GET statuses/user_timeline
//	https://dev.twitter.com/docs/api/1/get/statuses/user_timeline
function getTweets(screenName, tweetCount, outputTemplate, outputLocation)
{
	var destinationObject = jQuery("#"+outputLocation);
	if (destinationObject.length == 0){
		return false;
	}
	
	var haveTweets = false;
	
	var apiURL = "http://api.twitter.com/1/statuses/user_timeline/" + screenName + ".json?callback=?&include_rts=true";
	var jsonRequest = jQuery.getJSON(apiURL, function(data) {
		
		var carryOn = true;
		var i = 0;
		var output = "";
		var total = 0;
		var total = parseInt(data.length);
		
		if (!isNaN(total)){
			if (total>0){
				haveTweets = true;
			}
		}
		
		while ((i < tweetCount)&&(carryOn)){
			
			var tweet = data[i].text;
			var tweetDate = data[i].created_at;
			
			switch (outputTemplate){
				case "li":
				case "ul":
				case "ol":
					//output = output + "<li class='getTweetsTweet" + i + "'>" + activateURLs(tweet) + "<span class='getTweetsTweetDate'>" + data[i].created_at + "</span></li>";
					output = output + "<li class='getTweetsTweet" + i + "'>" +  activateURLs(tweet) + "</li>";
					break;
				case "ulMercato": // Used on the Mercato site ITelligence Feed no ActivateURLs on tweets
					output = output + "<li class='getTweetsTweet" + i + "'><a href='http://www.twitter.com/"+screenName+"' title='www.twitter.com/"+screenName+"' target='_blank'>" +  tweet + "</a></li>";
					break;
				case "divP":
				case "p":
					output = output + "<p class='getTweetsTweet" + i + "'>" + activateURLs(tweet) + "</p>";
					break;
				case "divSpan":
				case "span":
					output = output + "<span class='getTweetsTweet" + i + "'>" + activateURLs(tweet) + "</span>";
					break;
				case "div":
					output = output + "<div class='getTweetsTweet" + i + "'>" + activateURLs(tweet) + "</div>";
					break;
				default:
					output = output + tweet;
			}
			
			i++;
			if (i == data.length){
				carryOn = false;
			}
		}
		
		switch (outputTemplate){
			case "ulMercato":
			case "ul":
				output = "<ul>" + output  + "</ul>"
				break;
			case "ol":
				output = "<ol>" + output + "</ol>"
				break;
			case "divSpan":
			case "divP":
			case "div":
				output = "<div>" + output + "</div>"
				break;
		}
		
		jQuery(destinationObject).html(output);	
		
	});
	
	if(!haveTweets){
		var failedOutput = "";
		failedOutput = "<p>Follow <a href='http://www.twitter.com/"+screenName+"' title='www.twitter.com/"+screenName+"' target='_blank'>@"+screenName+"</a> on Twitter for the latest tweets</p>";
		jQuery(destinationObject).html(failedOutput);
	}
		
}

$(window).load(function() {

	$('#banners').css('overflow', 'visible');

});
