﻿var $j = jQuery.noConflict();

jQuery(document).ready(function($){

	var Apple = {};  
	Apple.UA = navigator.userAgent;  
	Apple.Device = false;  
	Apple.Types = ["iPhone", "iPod", "iPad"];  
	for (var d = 0; d < Apple.Types.length; d++) {  
		var t = Apple.Types[d];  
		Apple[t] = !!Apple.UA.match(new RegExp(t, "i"));  
		Apple.Device = Apple.Device || Apple[t];  
	}
	
	//not for iDevices!
	if(!Apple.Device) {
		var wysyctrl = {
			strikeThrough : { visible : true },

			separator01 : { visible : true },

			indent  : { visible : true },
			outdent : { visible : true },


			separator03 : { visible : true },

			undo : { visible : true },
			redo : { visible : true },

			separator04 : { visible : true },

			insertOrderedList    : { visible : true },
			insertUnorderedList  : { visible : true },

			h1mozilla : { visible : false },
			h2mozilla : { visible : false },
			h3mozilla : { visible : false },

			h1 : { visible : false },
			h2 : { visible : false },
			h3 : { visible : false },

			cut   : { visible : true },
			copy  : { visible : true },
			paste : { visible : true }
		}
		$('.dealCommentMsg').wysiwyg({
			css : { fontFamily:'Verdana, Arial, Helvetica, sans-serif', fontSize:'13px', border:'0px' },
			controls: wysyctrl
		});
		$('#txtMessage').wysiwyg({
			css : { fontFamily:'Verdana, Arial, Helvetica, sans-serif', fontSize:'13px', border:'0px' },
			controls: wysyctrl
		});
	    
		$(".wysiwyg").resizable({ minHeight: 220 }).css({'height':'220px'});
		$(".wysiwyg iframe").css({'height':'87%', 'width':'100%', 'min-height':'190px'}).attr('frameborder', '0').attr('marginwidth', '0').attr('marginheight', '0');
    }
    
    //set up readmore link
    $('.dealReadMore').live('click', function() {
		$(this).parent().height('auto');
		$(this).hide();
	});
    
    //set up comments expander
    $('.dealText').each(function(i,e) {
		if ($(e).height() > 148) {
			$(e).css({height:'148px'});
			$(e).prepend('<a class="dealReadMore">read more</a>');
			$(e).children('.dealReadMore').width($(e).width());
		}
	});
	
	//testemonials expander
	$('.dealUsers').each(function(i,e) {
		$(e).prepend('<a class="dealReadMore">read more</a>');
		$(e).children('.dealReadMore').width($(e).width()-36);
	});
	
	//comments editing
	$('.dealTextEditable a').click(function() {
		$(this).parent().hide('slow').prevAll('.dealText').hide('slow').nextAll('.dealTextEdit').css({ 'height':'auto', 'overflow-y':'auto', 'overflow-x':'hidden'}).slideDown('slow');
	});
	$('.dealTextEdit').css({ 'height':'auto', 'display':'none' });
});

//Google Analytics action tracking
function dealTrack(category, action, label, value) {
	try {
		_gaq.push(['_trackEvent', category, action, label, value]);
	} catch(e) { }
}

function Expandax(link) {
	$j(link).fadeOut('slow').next().fadeIn('slow');
}

function doSample(url, strDropdown) {
	var objIfr = $j('#' + strDropdown).closest('.dealBoxMiniInPage').next('iframe').get(0);
	objIfr.style.display = objIfr.style.display == 'none' ? 'block' : 'none';
	if (objIfr.style.display == 'block') objIfr.src = url;
}

function dealBtnOver(objAnc) {
	window.status = objAnc.title;
}

function dealBtnOut() {
	window.status = '';
}

function startTick(hh, mm, ss) {
	setInterval('dealTick("' + hh + '", "' + mm + '", "' + ss + '")', 1000)
}

function dealTick(hh, mm, ss) {
	function getInt(str) {
		str = str.replace(/(<([^>]+)>)/ig,"");
		if (str.indexOf('0') == 0) {
			str = str.substring(1);
		}
		return parseInt(str)
	}

	var objHour = document.getElementById(hh);
	var objMinute = document.getElementById(mm);
	var objSecond = document.getElementById(ss);
	var i;
	try {
		if (getInt(objSecond.innerHTML) > 0) {
			i = getInt(objSecond.innerHTML) - 1;
			objSecond.innerHTML = (i<10 ? '0' : '') + i;
		} else {
			objSecond.innerHTML = '59';
			if (getInt(objMinute.innerHTML) > 0) {
				i = getInt(objMinute.innerHTML) - 1;
				objMinute.innerHTML = (i<10 ? '0' : '') + i;
			} else {
				objMinute.innerHTML = '59';
				if (getInt(objHour.innerHTML) > 0) {
					i = getInt(objHour.innerHTML) - 1;
					objHour.innerHTML = (i<10 ? '0' : '') + i;
					
				} else {
					document.location.reload();
				}
			}
		}
		//Warning
		if ((getInt(objHour.innerHTML) == 0) && (getInt(objMinute.innerHTML) < 15)) {
			var FlashBack = false;
			if (getInt(objMinute.innerHTML) < 5) FlashBack = true;
			if ((getInt(objSecond.innerHTML)/2) == (parseInt(getInt(objSecond.innerHTML)/2))) {
				objHour.style.color = '#A31515';
				objMinute.style.color = '#A31515';
				objSecond.style.color = '#A31515';
				if (FlashBack) objHour.parentNode.parentNode.style.backgroundColor = '';
			} else {
				objHour.style.color = '';
				objMinute.style.color = '';
				objSecond.style.color = '';
				if (FlashBack) objHour.parentNode.parentNode.style.backgroundColor = '#FFDFD7';
			}
		}
	} catch(e) {}
}

function popupFriend(url) {
	window.open(url, 'help', 'width=450,height=620,status=no,location=no,scrollbars=yes');
}

function popupHelp(url) {
	window.open(url, 'help', 'width=400,height=350,status=no,location=no,scrollbars=yes');
}

function doPercentage(objRegularPrice, objDiscountPrice, objPercentageSaving) {
	var objReg = document.getElementById(objRegularPrice);
	var objDis = document.getElementById(objDiscountPrice);
	var objSpn = document.getElementById(objPercentageSaving);
	var intPercent = 0;
	if (objReg && objDis && objSpn) {
		if ((objReg.value.length > 0) && (objDis.value.length > 0)) {
			intPercent = Math.ceil(100 - objDis.value / objReg.value * 100);
		}
		switch (true) {
			case (intPercent < 30):
				objSpn.innerHTML = intPercent + '% <small style="font-weight:bold;color:#E60000">Too Low</small>';
				break;
			case ((intPercent >= 30) && (intPercent < 40)):
				objSpn.innerHTML = intPercent + '% <small style="font-weight:bold;color:#F39302">Poor</small>';
				break;
			case ((intPercent >= 40) && (intPercent < 45)):
				objSpn.innerHTML = intPercent + '% <small style="font-weight:bold;color:#02C519">OK</small>';
				break;
			case ((intPercent >= 45) && (intPercent < 50)):
				objSpn.innerHTML = intPercent + '% <small style="font-weight:bold;color:#02C519">Good</small>';
				break;
			case ((intPercent >= 50) && (intPercent < 60)):
				objSpn.innerHTML = intPercent + '% <small style="font-weight:bold;color:#02C519">Very Good</small>';
				break;
			case ((intPercent >= 60)):
				objSpn.innerHTML = intPercent + '% <small style="font-weight:bold;color:#02C519">Excellent!</small>';
				break;
		}
	}
}


function doCommission(objCommission, objCommissionNote) {
	var objComm = document.getElementById(objCommission);
	var objSpn = document.getElementById(objCommissionNote);
	var intComm = 0;
	if (objComm && objSpn) {
		if (objComm.value.length > 0) {
			intComm = objComm.value;
		}
		switch (true) {
			case (intComm < 30):
				objSpn.innerHTML = '<small style="font-weight:bold;color:#E60000">Too Low</small>';
				break;
			case ((intComm >= 30) && (intComm < 35)):
				objSpn.innerHTML = '<small style="font-weight:bold;color:#02C519">OK</small>';
				break;
			case ((intComm >= 35) && (intComm < 45)):
				objSpn.innerHTML = '<small style="font-weight:bold;color:#02C519">Good</small>';
				break;
			case ((intComm >= 45) && (intComm < 55)):
				objSpn.innerHTML = '<small style="font-weight:bold;color:#02C519">Very Good</small>';
				break;
			case ((intComm >= 55)):
				objSpn.innerHTML = '<small style="font-weight:bold;color:#02C519">Excellent!</small>';
				break;
		}
	}
}

function RevealOldComments(cid) {
	$j('#ancOldComments_'+cid).hide();
	$j('#ancOldCommentsHide_'+cid).css({display:'inline'}).show();
	$j('#ulOldComments_'+cid).slideDown('slow');
}
function HideOldComments(cid) {
	$j('#ancOldComments_'+cid).css({display:'inline'}).show();
	$j('#ancOldCommentsHide_'+cid).hide();
	$j('#ulOldComments_'+cid).slideUp('slow');
}