//all Quanat JS

// add overFlow marker onLoad

function overFlowMarker() {

	var a = "";
	var linkid = "";
	var link = "";
	jQuery(".news_description_text")
			.each(
					function() {
						jQuery(this).TooLong( {
							'len' : 420,
							'cutter' : '>>>>>>'
						});
						linkid = "orignalStoryLink" + jQuery(this).attr("ref");

						link = jQuery("#" + linkid).find("a").attr("href");
						a = jQuery(this).html();
						a = a
								.replace(
										"&gt;&gt;&gt;&gt;&gt;&gt;",
										"<a href ='#' id='_moreLink' class='readMore orignalLink ' style='border:none;'>...<img src='/web/images/icons/more.png'/></a>");
						a.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g, '\\$1');
						jQuery(this).html(a);
						//jQuery(this).find("a.orignalLink").attr("href",link);
						jQuery(this).find("a.orignalLink").attr(
								"href",
								'javascript:showRightSlider("' + jQuery(this)
										.attr("ref") + '");');

					});

}

// Queue,Fav and History overFlow Marker
function overFlowMarkerForOthers() {

	var a = "";
	var linkid = "";
	var link = "";
	jQuery(".news_description_text")
			.each(
					function() {
						jQuery(this).TooLong( {
							'len' : 420,
							'cutter' : '>>>>>>'
						});
						linkid = "orignalStoryLink" + jQuery(this).attr("ref");

						link = jQuery("#" + linkid).find("a").attr("href");
						a = jQuery(this).html();
						a = a
								.replace(
										"&gt;&gt;&gt;&gt;&gt;&gt;",
										"<a href ='#' id='_moreLink' class='readMore orignalLink ' style='border:none;'>...<img src='/web/images/icons/more.png'/></a>");
						a.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/])/g, '\\$1');
						jQuery(this).html(a);
						jQuery(this).find("a.orignalLink").attr("href", link);

					});

}

// Queue,Fav and History div toggle

function showRightSlider(p) {
	jQuery('#notepadz' + p).animate( {
		height : 'toggle'
	});
}

function toggleShareDiv(p) {

	jQuery('#_shareDiv' + p).fadeToggle();
}

// click anyware and hide popups code
function bodyClick() {

	jQuery("body")
			.click(function(e) {
				//you can then check what has been clicked
					var target = jQuery(e.target);
					var sucessFlag = false;
					//alert(target.parents().length);
					jQuery(target.parents())
							.each(
									function() {

										if (jQuery(this).attr("id") == "signin_container"
												|| jQuery(this).attr("class") == "right_pull_2"
												|| jQuery(this).attr("id") == "search_panel"
												|| jQuery(this).attr("id") == "search_logic_container") {

											sucessFlag = true;
										}
									});
					if (sucessFlag || target.is("#signIn")
							|| target.is("#_moreLink") || target.is("#moreBtn")) {

					} else {
						hideAllpopUps();

					}
				});
}

// hide every pop up div

function hideAllpopUps() {
	jQuery("#activelistsignin").hide();
	jQuery("#signin_container").hide();
	jQuery("#search_panel").hide();
	var stringValue = document.getElementById('headerFrm:in').value;
	if (stringValue != "") {
		jQuery("#moreBtn").attr("class", "more_btn");
	}

	jQuery(".right_pull_2").slideUp('slow');
	jQuery("#searchResultContainer").hide();

}

// topStories link on Home page

function topstoryLink() {
	if (jQuery("#top_stories_popup_list").css("display") == "none") {

		jQuery("#top_stories_popup").removeClass("top_stories_popup").addClass(
				"top_stories_popup_selected");

		jQuery("#top_stories_popup")
				.removeClass("top_stories_popup_selected_s").addClass(
						"top_stories_popup_selected");

		jQuery("#top_stories_popup_list").slideToggle("slow");
	} else {
		jQuery("#top_stories_popup_list").slideToggle("fast");
		window.setTimeout(function() {
			var class_name;
			if (jQuery("#story_top li a").attr("class") == "selected") {
				class_name = "top_stories_popup";

			} else {
				class_name = "top_stories_popup_selected_s";
			}
			jQuery("#top_stories_popup").removeClass(
					"top_stories_popup_selected").addClass(class_name);
		}, 200);

	}
}

//Show sign Up div

function showSignUpForm() {
	jQuery("#sign_in_container").modal( {
		onOpen : function(dialog) {
			dialog.overlay.fadeIn('slow', function() {
				dialog.data.hide();
				dialog.container.fadeIn('slow', function() {
					dialog.data.slideDown('slow');
				});
			});
		}
	});
}

function showSignInScreen() {
	jQuery('#signIn').bind('click', function(event) {
		jQuery("#activelistsignin").toggle();
		jQuery("#signin_container").toggle();
	});

	jQuery('#activelistsignin').bind('click', function(event) {
		jQuery("#activelistsignin").toggle();
		jQuery("#signin_container").toggle();
	});
}

function sortable() {

	jQuery("#contentLeft #mainChannelContainer").sortable( {
		opacity : 0.6,
		cursor : 'move',
		update : function() {
			var order = jQuery(this).sortable("serialize");

			//alert(order);
		var orignalOrder = order.replace(/recordsArray\[\]\=/gi, "");
		//alert(orignalOrder);
		var orignalOrder = orignalOrder.replace(/&/gi, ",");
		//alert(orignalOrder);
		jQuery("input[id$='frm:userSortedList']").val(orignalOrder);
		jQuery("input[id$='frm:userSortedList']").click();
	}
	});
}

function hideMe(p) {

	jQuery("#recordsArray_" + p + "").remove();

}

function UIBlock(data) {
	showIndicatorRegion(data);
}

function showIndicatorRegion(data) {
	if (data.status == "begin") {
		showElement();
	} else if (data.status == "success") {
		hideElement();
	}

}

function showElement() {
	jQuery.blockUI( {
		css : {
			backgroundColor : '#2b2b2b',
			color : '#fff'
		}
	});
}

function hideElement() {
	jQuery.unblockUI();
}

function showElementForSearch() {
	showSearchPanel();
	jQuery("#ajaxImg").show();
	jQuery("#ajaxDataContainer").hide();
}


function hideElementForSearch() {
	//alert("hide");
	jQuery("#ajaxImg").hide();
	jQuery("#ajaxDataContainer").show();
}

function showSearchPanel(){

	jQuery("#search_panel").toggle();
}

function addLoadify(p) {

	jQuery("#img" + p + " img").attr("src", "images/icons/ajaxloading.gif");

}

function removeLoadify(p, q, r) {

	var str = "";
	if (q == '1') {
		jQuery("#img" + p + " img")
				.attr("src", "images/icons/cancel_round.png");
		str = "added";
	} else {
		jQuery("#img" + p + " img").attr("src", "images/icons/plus.png");
		str = "removed";
	}
	jQuery("#update_msg").text("\"" + r + "\" channel " + str);
	jQuery("#searchResultContainer").hide();
	setTimeout("clearUpdateMsg()",3000);
}

function clearUpdateMsg(){
	jQuery("#update_msg").text("");
}
/*------Congratulation form popup---------*/
function showCongPopUp() {

	if (jQuery("#congrats_container").attr("id") != undefined) {

		jQuery("#congrats_container").modal( {
			onOpen : function(dialog) {
				dialog.overlay.fadeIn('slow', function() {
					dialog.data.hide();
					dialog.container.fadeIn('slow', function() {
						dialog.data.slideDown('slow');
					});
				});
			},
			onClose : function(dialog) {

				jQuery("a[id$='congratulationForm:selectChannels']").click();
				//	jQuery.modal.close(); // must call this!

		}
		});

	}

}

/* popUp for registration needed*/

function needRegistrationPopUp() {

	jQuery("#reg_popup").modal();
	//jQuery.modal('<div style="width:300px; height:30px; background-color: #eb823d;border:2px solid;"><div style="margin:auto; text-align:center;padding-top:7px;"><b>Need Registration</b></div></div>');

}

/*-----------tabs js------------*/
function loadTabsjs(){
	
	jQuery(".contents").hide(); //Hide all content
	jQuery("#right_tabs ul#tabnav li:first").addClass("selected").show(); //Activate first tab
	jQuery(".contents:first").show(); //Show first tab content

	
	
	//On Click Event
	jQuery("#right_tabs ul#tabnav li").live('click', function() {

	jQuery("#right_tabs ul#tabnav li").removeClass("selected"); //Remove any "active" class
	jQuery(this).addClass("selected"); //Add "active" class to selected tab
	jQuery(".contents").hide(); //Hide all tab content

	var activeTab = jQuery(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		jQuery(activeTab).show(); //Fade in the active ID content
	
		
	return false;
	});
}


//----------------------Show Share Email Box------------------------------------------------------------

// Replace errro msg in share Email

function ReplaceErrorMsg() {
	
	//alert(jQuery("#captchaMsg").text().trim());
	
	var str=jQuery("#captchaMsg").text().trim();
	var pattr=/does not match/gi;
	if (str.match(pattr)) {
		jQuery("#captchaMsgDiv").text("inccorect Code");
	} else {
		jQuery("#captchaMsgDiv").text(jQuery("#captchaMsg").text());
	}
	jQuery("#captchaMsg").text("");
}

function bindShareEmailLink() {
	jQuery('.google2').bind('click', function(event) {
		//alert(jQuery(this).attr("rel"));
		jQuery("#email_news_title").text(jQuery(this).attr("headline"));
		jQuery("#emailSubject").val(jQuery(this).attr("headline"));
		jQuery("#link").val(jQuery(this).attr("link"));
		
		jQuery("#emailPopUp_container").modal( {persist :true,
			onOpen : function(dialog) {
				dialog.overlay.fadeIn('slow', function() {
					dialog.data.hide();
					dialog.container.fadeIn('slow', function() {
						dialog.data.slideDown('slow');
					});
				});
			},onClose:function(dialog){resetAllFields();jQuery.modal.close();}
		});
	});
}

function successMsg(){
	//alert(jQuery("#callbackMsg li").text());
	if(jQuery("#callbackMsg li").text().trim() == "true" || jQuery("#callbackMsg li").text().trim() == "false"){
		//alert(jQuery("#callbackMsg li").text());
		jQuery("#successMsgDiv").text("Your Email has been sent.");
		jQuery("#successMsgDiv").show();
		window.setTimeout(function() {
			jQuery.modal.close();
		}, 3000);
	}
}

function resetAllFields(){
	jQuery("#successMsgDiv").text("");
	jQuery("#successMsgDiv").hide();
	jQuery("#emailPopUp_container form").get(0).reset();
	jQuery("#emailTo").val("");
	jQuery("#emailBcc").val("");
	jQuery("#emailFrom").val("");
	jQuery("#emailMsg").val("");
	  
}

//--------------------------------main method------------------------

jQuery(document).ready(function() {
	//load right side top stories tab
	loadTabsjs();
	//bind signIn Screen
		showSignInScreen();
	//bind body click event
		bodyClick();
	//bind share email link
		bindShareEmailLink();
});

