//remove conflict from prototype
var $j = jQuery.noConflict();

// ========================== for add to or delete from favourite
// =====================================
function addToFav(id, vid, uid) {
	// alert("add id" + id + "vid " + vid + "uid " + uid);
	// $('#'+id).hide();
	if (uid > 0) {
		$j('#heart_' + id).load("addFavourite.php", {
			vid :vid,
			uid :uid
		});
		$j('#added_' + id).show("slow");

		alert("Added to your favourite");
	} else {
		alert("Please Login first");
	}

	// document.getElementById("heart_"+id).style.display = "none";
	// $('#added_' + id).hide(4000);

	// $('#cross_' + id).show(4000);
	// document.getElementById("cross_"+id).style.display = "block";
}

function deleteFromFav(id, vid, uid) {
	// alert("delete id" + id + "vid " + vid + "uid " + uid);
	if (uid > 0) {
		var answer = confirm("Please confirm removing ringtone from favorites list?");
		if (answer) {
			$j('#cross_' + id).load("deleteFavourite.php", {
				vid :vid,
				uid :uid
			});
			$j('#deleted_' + id).show("slow");
		}

	} else {
		alert("Please Login first");
	}
	// document.getElementById("heart_"+id).style.display = "none";
	// $('#deleted_' + id).hide(4000);
	//	
	// $('#heart_' + id).show(4000);
}

function addToFavFromSubscr(id, vid, uid) {
	// $('#'+id).hide();

	if (uid > 0) {

		$j('#sub_heart_' + id).load("addFavourite.php", {
			vid :vid,
			uid :uid
		});

		$j('#sub_added_' + id).show("slow");
		alert("Added to your favourite");
	} else {
		alert("Please Login first");
	}
	// document.getElementById("heart_"+id).style.display = "none";
	// $('#added_' + id).hide(4000);

	// $('#cross_' + id).show(4000);
	// document.getElementById("cross_"+id).style.display = "block";
}

// delete ringtone file and clear relative data: comment, comment vote
function deleteRingtone(id, vid, uid) {
	// alert("delete ringtone id" + id + "vid " + vid + "uid " + uid);
	if (uid > 0) {
		var answer = confirm("Do you want to delete your ringtone?");
		if (answer) {
			$j('#cross_' + id).load("deleteRingtone.php", {
				vid :vid,
				uid :uid
			});
			$j('#deleted_' + id).show("slow");
		}
	} else {
		alert("Please Login first");
	}

}

// ******************************** for thumb voting
// ****************************************8
function voteOverUp(id) {
	// alert(id);
	// $j('#handup_' + id).hide();
	// $j('#handupover_' + id).show();

	document['up_' + id].src = "images/icons/handupover.jpg";

	// document.getElementById('handup_' + id).style.display = "none";
	// document.getElementById('handupover_' + id).style.display = "block";
}

function voteOutUp(id) {
	document['up_' + id].src = "images/icons/handup.gif";
	// alert(id);
	// document.getElementById('handupover_' + id).style.display = "none";
	// document.getElementById('handup_' + id).style.display = "block";
}

function voteOverDown(id) {
	// $j('#handdown_' + id).hide();
	// $j('#handdownover_' + id).show();
	document['down_' + id].src = "images/icons/handdownover.jpg";
	// document.getElementById('handdown_' + id).style.display = "none";
	// document.getElementById('handdownover_' + id).style.display = "block";
}

function voteOutDown(id) {
	document['down_' + id].src = "images/icons/handdown.gif";
	// document.getElementById('handdown_' + id).style.display = "block";
	// document.getElementById('handdownover_' + id).style.display = "none";
}

function voteDown(id, comid, uid) {
	// alert(id + " - " + comid + " - " + uid );
	$j('#handdown_' + id).load("vote_update.php", {
		comid :comid,
		uid :uid,
		vote :-1
	});

	$j('#reply_' + id).load("vote_load.php", {
		comid :comid
	});
	// document['handdown_' + id].src = "images/icons/handdown.gif";
}

function voteUp(id, comid, uid) {
	// alert(id + " - " + comid + " - " + uid );
	$j('#handup_' + id).load("vote_update.php", {
		comid :comid,
		uid :uid,
		vote :1
	});

	$j('#reply_' + id).load("vote_load.php", {
		comid :comid
	});
	// $j('#handup_' + id).load( "http://localhost/images/icons/handup.gif" );
	// document['handup_' + id].src = "images/icons/handup.gif";
}

function updateVotes(id, comid) {
	$j('#reply_' + id).load("vote_load.php", {

		comid :comid
	});
}

// pagination
function changePage(pageId, reload_page) {

	currentPlayer = "";
	// alert( pageId + "_" + reload_page);

	// "templates/search_result/latest-upload.php"
	$j('#countrydivcontainer').load(reload_page, {
		pageNum :pageId,
		change :true
	});

	// alert( "done_reload");
	return false;
}


//pagination collection
function changePageCollection(pageId, reload_page) {
	currentPlayer = "";
	// alert( pageId + "_" + reload_page);

	// "templates/search_result/latest-upload.php"
	$j('#countrydivcontainer').load(reload_page, {
		pageNum :pageId,
		change :true
	});

	// alert( "done_reload");
	return false;
}


// pagination for sub
function changePageSub(pageId, reload_page, divId) {
	currentPlayer = "";
	// alert( pageId + "_" + reload_page + "_" + divId);

	// "templates/search_result/latest-upload.php"
	$j('#' + divId).load(reload_page, {
		pageNum :pageId,
		change :true
	});

	// alert( "done_reload");
	return false;
}

// pagination for comment
function changePageComment(pageId, reload_page, divId) {
	currentPlayer = "";
	// alert( pageId + "_" + reload_page + "_" + divId);

	$j('#' + divId).load(reload_page, {
		pageNum :pageId,
		change :true
	});

	// alert( "done_reload");
	return false;
}

// for ringtone pc download and hp download
var download = 0;

function lunchboxOpen(lunchID, start, end) {
	// alert(lunchID);
	// var i =0;
	/*
	 * for(i=start; i<=end; i++){ if(i!=lunchID){ lunchboxClose( i, start, end );
	 * }else{
	 */download = document.getElementById('lunch_' + lunchID);
	download.style.display = "block";
	// document.getElementById('clasp_' + lunchID).innerHTML="<a
	// href=\"javascript:lunchboxClose('" + lunchID +
	// "','"+start+"','"+end+"');\"><img
	// src='images/default/featured/download_btn.jpg'/></a>";
	// }
	// }
}

function lunchboxClose(lunchID, start, end) {
	document.getElementById('lunch_' + lunchID).style.display = "none";
	document.getElementById('clasp_' + lunchID).innerHTML = "<a href=\"javascript:lunchboxOpen('"
			+ lunchID
			+ "','"
			+ start
			+ "','"
			+ end
			+ "');\"><img src='images/default/featured/download_btn.jpg'/></a>";
}

function mclose() {
	if (download)
		download.style.display = 'none';
}

document.onclick = mclose;

function phone_download(uid, vid) {
	uid = parseInt(uid);

	if (uid + '' == 'NaN' || uid <= 0) {
		alert('Please login first to download ringtone to your phone');
		return false;
	}

	$j
			.post(
					"download_hp.php",
					{
						uid :uid,
						vid :vid
					},
					function(data) {
						// alert(data);
						if (data == 'yes') // if correct login detail
							alert('Ringtone added to your phone download list');
						else if (data == 'point')
							alert('You don\'t have enough points to download this ringtone');
						else if (data == 'exist')
							alert('You already have this ringtone in your phone download list');
						else
							alert('There is an error');
					});

}

function checkFileExt(type) {

	var file = document.getElementById("own_photo_upload").value;
	extArray = new Array(".jpg", ".png", ".gif");

	allowSubmit = false;

	// alert(file);

	if (!file) {
		if (type == "0")
			return true;

		alert("Please choose a file to upload");
		return false;
	}

	while (file.indexOf("\\") != -1)
		file = file.slice(file.indexOf("\\") + 1);

	ext = file.slice(file.lastIndexOf(".")).toLowerCase();

	// alert(ext);
	for ( var i = 0; i < extArray.length; i++) {
		if (extArray[i] == ext) {
			allowSubmit = true;
			break;
		}
	}

	if (allowSubmit)
		return true;
	else
		alert("Please only upload files that end in types:  "
				+ (extArray.join("  ")) + "\nPlease select a new "
				+ "file to upload and submit again.");
	return false;
}

function disableEnterKey(e) {
	var key;
	if (window.event)
		key = window.event.keyCode; // IE
	else
		key = e.which; // firefox

	return (key != 13);
}

function setCookieFav(url) {
	// alert(url);
	document.cookie = "countrytabs=1;path=/"; // cookie value is domain wide
	// (path=/)
	location.reload(true);
	// window.location = url;

	// window.navigate(url);
	// location.reload(url);
	// alert(document.cookie );
}