/* 
* WEISS MAIN JAVASCRIPT FILE
*
*/

/**
 * Misc
 */
jQuery.noConflict();

/**
 * Sign Up Widget
 */

//function setFocus(){
//document.getElementById('$focus').focus();
//}

function clearText(thefield){
	if (thefield.defaultValue==thefield.value) {
		thefield.value = "";
	}
}
function addTextFN(thefield){
	if (thefield.value=="") {
		thefield.value = "first name";
	}
}
function addTextE(thefield){
	if (thefield.value=="") {
		thefield.value = "e-mail address";
	}
}

/**
 * Add to Favorites
 */
function addToFavorites() {
    title = 'Weiss Research ' + document.title; 
    url = location.href;
    if (window.sidebar) // FIREFOX
    { 
	    alert("Press Ctrl-D to Add This page to your favorites...");
    } else if( window.external )  //IE
	    {  window.external.AddFavorite( url, title);}
	else if(window.opera && window.print) { // Opera Hotlist
		return true; 
	}
 }

/**
 * Text Resize
 */

function w_setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

function w_getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1) return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1) cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function changeFontSize(id, $obj) {
	$obj.css({'background-position' : '-'+w_fontsize_bg_offset_array[id]+'px -278px'});
    var untilDate = new Date();
    untilDate.setTime( untilDate.getTime() + 7 * 24 * 60 * 60 * 1000); // valid for 7 days
    w_setCookie('w_fontsize_id', id, untilDate, '/');
    self.location.href = self.location.href;
}

var w_styleElement="div#content p"; // the ID of the content DIV to resize
var w_fontsize_id = w_getCookie('w_fontsize_id');
if (!w_fontsize_id) var w_fontsize_id = 1;
var fontsize_array = new Array('', '100', '112', '120');
w_fontsize_bg_offset_array = new Array('', '800', '818', '836');
var w_fontsize = fontsize_array[w_fontsize_id];
var w_fontsize_bg_offset = w_fontsize_bg_offset_array[w_fontsize_id];

document.write('<style type="text/css">' + w_styleElement + '{font-size:'+w_fontsize+'% !important;} button#w_fontsize_'+w_fontsize_id+' { background-position: -'+w_fontsize_bg_offset+'px -278px !important; }<\/style>');

jQuery(document).ready(function($) {
    $('#w_fontsize_1').click(function () {
      changeFontSize('1', $(this));
      return false;
    });
    $('#w_fontsize_2').click(function () { 
      changeFontSize('2', $(this));
      return false;
    });
    $('#w_fontsize_3').click(function () {
      changeFontSize('3', $(this));
      return false;
    });
    $('#s').click(function () { 
      $(this).attr('value', '');

    });
});

/**
 * Stock Market Indexes
 */
jQuery( function() {
	jQuery(".w_smi_tr_index").click( function() {
		jQuery(this).attr("title", "");
		img = jQuery(this).next("tr").find("img:hidden");
		if (img.attr("src") != undefined) {
			ind = jQuery(this).attr("id").replace(/w_smi_/, "");
			img.attr("id", "w_smi_chart_" + ind);
			// build the URL for graphic chart
			// make corection
			if (ind == "SHSZ300") {
				ind = "399300.SZ";
			} else {
				ind = "^" + ind;
			}
			img.attr("src", "http://ichart.finance.yahoo.com/t?s=" + ind);
			img.slideDown(120);
		} else {
			img = jQuery(this).next("tr").find("img");
			img.slideUp(120);
		}
		return false;
	});
	jQuery(".w_smi_tr_chart img").click( function() {
		ind = jQuery(this).attr("id").replace(/w_smi_chart_/, "");
		if (ind == "SHSZ300") {
			ind = "399300.SZ";
		} else {
			ind = "^" + ind;
		}
		src = jQuery(this).attr("src");
		if (src.indexOf("/v?s") != -1) {
			x = "t";
			title = "Today";
		} else {
			x = "v";
			title = "5 days";
		}
		jQuery(this).attr("src", "http://ichart.finance.yahoo.com/" + x + "?s=" + ind);
		jQuery(this).attr("title", title);
		jQuery(this).attr("alt", title);
		return false;
	});
});