$(document).ready(initTextResizer);

var COOKIE_NAME = 'textCookie';
var DEFAULT_SIZE = '1.2em';
var MEDIUM_SIZE = '1.4em';
var LARGE_SIZE = '1.6em';
var DEFAULT_SIZE_MODULE = '1em';
var MEDIUM_SIZE_MODULE = '1.1em';
var LARGE_SIZE_MODULE = '1.2em';
/*h3*/
var DEFAULT_SIZE_H3 = '1.3em';
var MEDIUM_SIZE_H3 = '1.5em';
var LARGE_SIZE_H3 = '1.7em';

/**
 *	Initializes the text resizing functionality
 */
function initTextResizer() {
	// check cookie value
	if ($.cookie(COOKIE_NAME) != null) {
		switch ($.cookie(COOKIE_NAME)) {
			case 'medium':
			$("#main p, .module_home .text h3").css("font-size", MEDIUM_SIZE);
			$("#main #main .btn_modules").css("font-size", MEDIUM_SIZE_MODULE);
			$(".module_products_l2 h3").css("font-size", MEDIUM_SIZE_H3);
			$("#main #content ul li").css("font-size", MEDIUM_SIZE);
			$("ul.toggle_options li").css("font-size", DEFAULT_SIZE); //prevent from increasing
			$("div").filter(".sliding_description").css("top", "200px");
			$("#fontSize .moreFonts .sub_menu.ddRight li a").removeClass("selected");			
			$("#fontSize .moreFonts .sub_menu.ddRight li a#textMedium").addClass("selected");			
			break;
			
			case 'large':
			$("#main p, .module_home .text h3").css("font-size", LARGE_SIZE);
			$("#main .btn_modules").css("font-size", LARGE_SIZE_MODULE);
			$(".module_products_l2 h3").css("font-size", LARGE_SIZE_H3);			
			$("#main #content ul li").css("font-size", LARGE_SIZE);		
			$("ul.toggle_options li").css("font-size", DEFAULT_SIZE); //prevent from increasing				
			$("div").filter(".sliding_description").css("top", "195px");
			$("#fontSize .moreFonts .sub_menu.ddRight li a").removeClass("selected");			
			$("#fontSize .moreFonts .sub_menu.ddRight li a#textLarge").addClass("selected");			
			break;
		}
	}
	
	// text resizing
	$("a").filter("#textDefault").click(function(event) {
		event.preventDefault();
		$("#main p, .module_home .text h3").css("font-size", DEFAULT_SIZE);
		$("#main .btn_modules").css("font-size", DEFAULT_SIZE_MODULE);
		$(".module_products_l2 h3").css("font-size", DEFAULT_SIZE_H3);	
		$("#main #content ul li").css("font-size", DEFAULT_SIZE);	
		$("ul.toggle_options li").css("font-size", DEFAULT_SIZE); //prevent from increasing						
		$("div").filter(".sliding_description").css("top", "210px");
		$("#fontSize .moreFonts .sub_menu.ddRight li a").removeClass("selected");			
		$("#fontSize .moreFonts .sub_menu.ddRight li a#textDefault").addClass("selected");	
		//Cufon.refresh("#fontSize .moreFonts .sub_menu.ddRight li a");
		Cufon.replace('a#textMedium, a#textLarge', { 
			fontFamily: 'nevis',
			color: '#444444'
		});
		Cufon.replace('a#textDefault', { 
			fontFamily: 'nevis',
			color: '#ffffff'
		});		
		Cufon.refresh(".module_home #home_community #on-youtube .title p");
		Cufon.refresh(".module_home #home_community #recent-update .title p");
		// delete cookie since default
		$.cookie(COOKIE_NAME, null);
	});
	$("a").filter("#textMedium").click(function(event) {
		event.preventDefault();
		$("#main p, .module_home .text h3").css("font-size", MEDIUM_SIZE);
		$("#main .btn_modules").css("font-size", MEDIUM_SIZE_MODULE);
		$(".module_products_l2 h3").css("font-size", MEDIUM_SIZE_H3);	
		$("#main #content ul li").css("font-size", MEDIUM_SIZE);
		$("ul.toggle_options li").css("font-size", DEFAULT_SIZE); //prevent from increasing		
		$("div").filter(".sliding_description").css("top", "200px");
		$("#fontSize .moreFonts .sub_menu.ddRight li a").removeClass("selected");			
		$("#fontSize .moreFonts .sub_menu.ddRight li a#textMedium").addClass("selected");		
		//Cufon.refresh("#fontSize .moreFonts .sub_menu.ddRight li a");
		Cufon.replace('a#textDefault, a#textLarge', { 
			fontFamily: 'nevis',
			color: '#444444'
		});
		Cufon.replace('a#textMedium', { 
			fontFamily: 'nevis',
			color: '#ffffff'
		});
		Cufon.refresh(".module_home #home_community #on-youtube .title p");		
		Cufon.refresh(".module_home #home_community #recent-update .title p");		
		$.cookie(COOKIE_NAME, 'small');
	});
	$("a").filter("#textLarge").click(function(event) {
		event.preventDefault();
		$("#main p, .module_home .text h3").css("font-size", LARGE_SIZE);
		$("#main .btn_modules").css("font-size", LARGE_SIZE_MODULE);
		$(".module_products_l2 h3").css("font-size", LARGE_SIZE_H3);
		$("#main #content ul li").css("font-size", LARGE_SIZE);	
		$("ul.toggle_options li").css("font-size", DEFAULT_SIZE); //prevent from increasing					
		$("div").filter(".sliding_description").css("top", "195px");
		$("#fontSize .moreFonts .sub_menu.ddRight li a").removeClass("selected");			
		$("#fontSize .moreFonts .sub_menu.ddRight li a#textLarge").addClass("selected");		
		//Cufon.refresh("#fontSize .moreFonts .sub_menu.ddRight li a");	
		Cufon.replace('a#textDefault, a#textMedium', { 
			fontFamily: 'nevis',
			color: '#444444'
		});
		Cufon.replace('a#textLarge', { 
			fontFamily: 'nevis',
			color: '#ffffff'
		});		
		Cufon.refresh(".module_home #home_community #on-youtube .title p");	
		Cufon.refresh(".module_home #home_community #recent-update .title p");		
		$.cookie(COOKIE_NAME, 'large');
	});
}

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/**
 *	Initializes the toggle tab content.
 */
function initToggleContent() {
	$("li.toggle").click(function() {
		// switch all tabs off
		$("li.active").removeClass("active");
		
		// switch this tab on
		$(this).addClass("active");
		
		// switch all contents off
		$("div.toggle_content").hide();
		
		// show selected content
		var content_show = $(this).attr("title");
		$("#"+content_show).show();
	});
	
	// show the first toggable tab and content
	$("ul.toggle_options").children().eq(0).addClass("active");
	$("div.toggle_content").eq(0).show();
}

/**
 *	Initializes the social box toggle tab content.
 */
function initToggleContentSocial() {
	$("a.toggleSocial").click(function() {
		//alert( $(this).attr("class").substring(13) );
		if( $(this).attr("class").substring(13) == "tab_twitter" ){
			//activate selected tab
			$(".tab_twitter").addClass("tab_twitter_current");			
			$(".tab_twitter").removeClass("tab_twitter");
			
			//deactivate other tabs
			$(".tab_blogs_current").addClass("tab_blogs");			
			$(".tab_blogs_current").removeClass("tab_blogs_current");
			$(".tab_forums_current").addClass("tab_forums");			
			$(".tab_forums_current").removeClass("tab_forums_current");
		}
		
		if( $(this).attr("class").substring(13) == "tab_blogs" ){
			//activate selected tab
			$(".tab_blogs").addClass("tab_blogs_current");			
			$(".tab_blogs").removeClass("tab_blogs");
			
			//deactivate other tabs
			$(".tab_twitter_current").addClass("tab_twitter");			
			$(".tab_twitter_current").removeClass("tab_twitter_current");
			$(".tab_forums_current").addClass("tab_forums");			
			$(".tab_forums_current").removeClass("tab_forums_current");			
		}
		
		if( $(this).attr("class").substring(13) == "tab_forums" ){
			//activate selected tab
			$(".tab_forums").addClass("tab_forums_current");			
			$(".tab_forums").removeClass("tab_forums");
			
			//deactivate other tabs
			$(".tab_twitter_current").addClass("tab_twitter");			
			$(".tab_twitter_current").removeClass("tab_twitter_current");
			$(".tab_blogs_current").addClass("tab_blogs");			
			$(".tab_blogs_current").removeClass("tab_blogs_current");
		}				
			
		// switch all contents off
		$("div.toggle_socialContent").hide();
		
		// show selected content
		var content_show = $(this).attr("title");
		$("#"+content_show).show();
	});
	
	// show the first toggable tab and content
	//$("li#toggle_option_1").eq(0).addClass("active");
	$("div#toggle_socialContent_1").show();
}

/**
 *	Initializes the expand tab content.
 */
function initExpandContent() {
	
	
	/* QUE SE EL PRIMER ELEMENTO, NO SIEMPRE CON EL ID 0 */
		
	var expandedItem = $("#home_news-and-events .item:eq(0)").find(".expand_content").attr("class");
	expandedItem = expandedItem.substring(15);
	
	var collapsed = false;
	
	//show first item by default
	$("#home_news-and-events .item #"+expandedItem).addClass("active");
	// show selected content
	$("."+expandedItem).show();	
	
	$("a.expand").click(function() {
		//if user clicks the opened item, then collapse								 
		if( ($(this).attr("title") ==  expandedItem) & (collapsed == false) ){
			//alert("the opened");
			$("#"+$(this).attr("title")).removeClass("active");
			$("div."+$(this).attr("title")).hide();
			
			//collapsed, if clicked again, expand
			collapsed = true;
		} else{									 
			//get btn title
			expandedItem = $(this).attr("title");
									 
			// switch all tabs off
			$("a.expand").removeClass("active");
			
			// switch this tab on
			$(this).addClass("active");
			
			// switch all contents off
			$("div.expand_content").hide();
			
			// show selected content
			var content_show = $(this).attr("title");
			$("."+content_show).show();	
			
			//expanded, ready to collapse
			collapsed = false;
			
		}//else							
	});//func	
}

function updateHTML(elmId, value) {
  document.getElementById(elmId).innerHTML = value;
}

function setytplayerState(newState) {
  updateHTML("playerstate", newState);
}

function onYouTubePlayerReady(playerId) {
  ytplayer = document.getElementById("myytplayer");
}

function onPlayerError(errorCode) {
  alert("An error occured: " + errorCode);
}

function onytplayerStateChange(newState) {
  setytplayerState(newState);
}

// functions for the api calls
function loadNewVideo(id, startSeconds) {
  if (ytplayer) {
	ytplayer.loadVideoById(id, parseInt(startSeconds));
  }
}

function getPlayerState() {
  if (ytplayer) {
	return ytplayer.getPlayerState();
  }
}

//function for hero thumb action
function heroThumbs(thumbTitle){
	//make selected thumb appear
	$("#"+thumbTitle).find("span").attr({
		"style": 'display:block; margin-top: 8px'
	});
	$("#"+thumbTitle).find("span").animate({marginTop: "8px"}, {queue:false, duration:400});
	$("#"+thumbTitle).find("span").animate({opacity: 1}, {queue:false, duration:400});	
	
	//make all thumbs the normal hover state
	$("#heroThumbs li").hover(
		function(){
			$(this).find("span").attr({
				"style": 'display:block'
			});
			$(this).find("span").animate({marginTop: "8px"}, {queue:false, duration:400});
			$(this).find("span").animate({opacity: 1}, {queue:false, duration:400});
		}, 
		function(){
			$(this).find("span").animate({marginTop: "0px"}, {queue:false, duration:400});			
			$(this).find("span").animate({opacity: 0}, {queue:false, duration:400}, "linear");
	});
	
	//only the selected thumb will show even on hover off
	$("#"+thumbTitle).hover(
		function(){
			$(this).find("span").attr({
				"style": 'display:block; margin-top: 8px'
			});
			$(this).find("span").animate({marginTop: "8px"}, {queue:false, duration:400});
			$(this).find("span").animate({opacity: 1}, {queue:false, duration:400});
		}, 
		function(){
		$(this).find("span").animate({marginTop: "8px"}, {queue:false, duration:400});			
		$(this).find("span").animate({opacity: 1}, {queue:false, duration:400}, "linear",
			function(){
				$(this).find("span").attr({"style": 'display:none'});			
			}
		);
	});
}

//keep autorotation going
function autoRotation(thumbIndex){	
	setTimeout( function() {
		if(thumbIndex == 4){
			thumbIndex = -1;
		}
		$('#heroThumbs li a').eq(thumbIndex+1).trigger('click'); 
		autoRotation(thumbIndex + 1);//call next slide
	}, 5000);
}

//clear input fields
(function($){
  $.fn.clearDefault = function(){
	  return this.each(function(){	
			  var default_value = $(this).val();			
			  $(this).focus(function(){			
				  if ($(this).val() == default_value) $(this).val("");			
			  });
		
			  $(this).blur(function(){			
				  if ($(this).val() == "") $(this).val(default_value);			
			  });		
	  });	
  };
})(jQuery);

