$(function() {
	$('#hk_security a').click(function () {
		var messages = ["https://top.chiefgroup.com.hk/web/logon.do", "https://top2.chiefgroup.com.hk/web/logon.do", "https://top3.chiefgroup.com.hk/web/logon.do"];
		var href = messages[Math.floor(Math.random() * messages.length)];
		$(this).attr('href', href).attr('target', '_blank');
	});
	$('a[rel=popup_activies]').click(function () {
		window.open ($(this).attr('href'),"popup","width=670,scrollbars=1,resizable=1");
		return false;
	});
	var loaded = 0;
	$('#news').cycle({timeout:6000, fx:'scrollUp', pause:1});
	$('#slides').each(function() {
		var $ul = $(this);
		var $liArr = $ul.children('li');
		$liArr.sort(function(a,b) {
			var temp = parseInt( Math.random()*1000 );
			var isOddOrEven = temp%2;
			var isPosOrNeg = temp>5 ? 1 : -1;
			return( isOddOrEven*isPosOrNeg );
		}).appendTo($ul);
	});
	$('#slides').cycle({
		width:'687',
		height:'244',
		fit: '1',
		fx:'fade',
		speed:'slow',
		timeoutFn: function (currSlideElement, nextSlideElement, options, forwardFlag) {
			return parseInt($(currSlideElement).find('img').attr('duration'));
		},
		pause:1,
		slideExpr:'li',
		pager:  '#thumb',
		after : function (curr, next, opts) {
			if (loaded == 1) {
				var page  = opts.currSlide;
				$("#thumb").data('jcarousel').scroll(parseInt(page));
			}
		},
	    pagerAnchorBuilder : function(idx, slide) {
	    	var rand = Math.round(Math.random());
	    	return '<li><a href="#" style="background-image:url('+$(slide).find('img').attr('thumb')+'?rand='+ rand + ');">'+idx+'</a><span class="line">&nbsp;</span></li>';
	    }
	});
	$('#prev_btn').bind('click', function() {
		$("#thumb").data('jcarousel').prev();
		return false;
	});
	$('#next_btn').bind('click', function() {
		$("#thumb").data('jcarousel').next();
		return false;
	});
	$('#next_btn').mousedown(function() {
		$("#prev_btn").stopTime();
		$('#slides').cycle('pause');
		$(this).everyTime(100, function() {
			$("#thumb").data('jcarousel').next();
		});
	}).mouseup(function() {
		$('#slides').delay(500).cycle('resume');
		$(this).stopTime();
	});
	$('#prev_btn').mousedown(function() {
		$("#next_btn").stopTime();
		$('#slides').cycle('pause');
		$(this).everyTime(100, function() {
			$("#thumb").data('jcarousel').prev();
		});
	}).mouseup(function() {
		$('#slides').delay(500).cycle('resume');
		$(this).stopTime();
	});
	$(window).bind('load', function() {
		$("#research_thumb").css('backgroundImage', "url('"+$("#research_thumb").attr('background')+"')");
		$("#thumb").jcarousel({
			speed : 100,
			start : 1,
			scroll : 1,
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: function(jc, state) {
				loaded = 1;
			}
		});
	});
	$("#thumb li a, #next_btn, #prev_btn").bind('mouseover', function () {
		$('#slides').cycle('pause');
	}).bind('mouseout', function () {
		$('#slides').cycle('resume');
	});
	$("#stock_form").submit(function () {
		var numberRegex = /^[+-]?\d+(\.\d+)?([eE][+-]?\d+)?$/;
		if (!numberRegex.test($(this).find("input[name=id]").val())) {
			alert("請輸入股票代號");
			$(this).find("input[name=id]").focus();
			return false;
		}
		$(this).attr('target', '_self');
	});
	$("#stock_form_btn").bind('click', function() {
		$("#stock_form").submit();
	});
	var height = 0;
	$("#talk_list").children('li').each (function (i) {
		if (i < $("#talk_list li").length-1) {
			height += 7;
		}
		height += parseInt($(this).css('height'));
		if (height > parseInt($("#talk_list").css('height'))) {
			$(this).remove();
		}
	});
	$("#research_heading a").bind('click', function () {
		$('#ajax_research a').removeClass('selected');
		$("#research_list").html($("#info_01").html());
		var height = 0;
		$("#research_list").children('li').each (function (i) {
			if (i < $("#research_list li").length-1) {
				height += 7;
			}
			height += parseInt($(this).css('height'));
			if (height > parseInt($("#research_list").css('height'))) {
				$(this).remove();
			}
		});
		$(this).parent().parent().find('#more_btn a').attr('href', 'http://www.chiefgroup.com.hk/research/r_report.php');
		return false;
	});
	$("#link a:last, #social a, #research_list a, #talk_list a, #new_list a, #news a, #appointment a, #download a, #us_security a, #branch a, #pps a, #auto a, #daily_btn a").live('click', function () {
		$(this).attr('target', '_blank');
	});
	$("#download a, #branch a, #news_btn a,").live('click', function () {
		$(this).attr('target', '_self');
	});
	$("#ajax_research a").bind('click', function () {
		$('#ajax_research a').removeClass('selected');
		$(this).addClass('selected');
		$("#research_list").html($("#"+$(this).attr('rel')).html());
		var height = 0;
		$("#research_list").children('li').each (function (i) {
			if (i < $("#research_list li").length-1) {
				height += 7;
			}
			height += parseInt($(this).css('height'));
			if (height > parseInt($("#research_list").css('height'))) {
				$(this).remove();
			}
		});
		var messages = ["http://www.chiefgroup.com.hk/research/r_report.php", "http://www.chiefgroup.com.hk/research/r_daily.php", "http://www.chiefgroup.com.hk/research/r_stockanalysis.php", "http://www.chiefgroup.com.hk/research/r_goldandoil.php"];
		$(this).parent().parent().parent().parent().find('#more_btn a').attr('href', messages[$("#ajax_research a").index(this)]).attr('target', '_blank');
		return false;
	});
	$("#research_list").html($("#info_01").html());
	$("#research_list").parent().find('#more_btn a').attr('href', 'http://www.chiefgroup.com.hk/research/r_report.php');
	var height = 0;
	$("#research_list").children('li').each (function (i) {
		if (i < $("#research_list li").length-1) {
			height += 7;
		}
		height += parseInt($(this).css('height'));
		if (height > parseInt($("#research_list").css('height'))) {
			$(this).remove();
		}
	});
	$("a[rel^='prettyPhoto']").prettyPhoto({social_tools:false, animation_speed:'fast',slideshow:10000, autoplay:false});
});
