// JavaScript Document


jQuery(function() {
    jQuery("p.dropcap").each(function() {
        var text = jQuery(this).html();
        var first = jQuery('<span>'+text.charAt(0)+'</span>').addClass('dropcap');
        jQuery(this).html(text.substring(1)).prepend(first);
    });
	loadLogin();
	jQuery(window).resize(function(){loadLogin('resized');});
	jQuery('input.domainChk').each(function(){
		if(jQuery(this).is(':checked')){
			jQuery(this).closest('div.inputbox').find('input.dtext').removeAttr('disabled');
		}
	});
});

jQuery('input.domainChk').live('click',function(){
	var v = jQuery(this);
	v.closest('dl').find('input.dtext').attr('disabled','disabled');
	v.closest('div.inputbox').find('input.dtext').removeAttr('disabled');
});

jQuery("#url, #alt-url").live('focusin focusout keyup keypress',function(event){
	if (jQuery(this).val()!="")
	{
		var v = jQuery(this);
		var url = jQuery.ajax({
			type:'post',
			url: base_path+"product/signup/jchecksubdomain/",
			data:'subdomain='+jQuery(this).val()+'&primary_domain='+jQuery("#primary_domain").val(),
			dataType:'json',
			success: function(json) {
				if(json.errorno == 0){
					v.closest('div.input').removeClass('error');
					v.closest('div.input').find('span.error').remove();
				}else {
					v.closest('div.input').addClass('error');
					if(v.closest('div.input').find('span.error').length > 0){
						v.closest('div.input').find('span.error').html(json.error);
					}else {
						v.closest('div.input').append('<span class="error">'+json.error+'</span>');
					}
				}
			}
		});

		if(event.type =='keypress'){
			if(!jQuery(this).closest('div.input').hasClass('pressed')){jQuery(this).closest('div.input').addClass('pressed')}
			url.abort();
		}
		if(event.type == 'focusin'){
			url.abort();
		}
	}
		if(event.type == 'focusin'){
			jQuery(this).closest('div.input').find('label').hide();
			jQuery(this).closest('div.input').addClass('focused');
		}
		if(event.type == 'focusout'){
			if(jQuery(this).val() == ''){
				jQuery(this).closest('div.input').find('label').show();	
			}
			jQuery(this).closest('div.input').removeClass('focused');	
		}
		if(event.type =='keyup'){
			if(jQuery(this).val() == ''){
				jQuery(this).closest('div.input').removeClass('pressed');	
			}
		}
});

jQuery("#homeGallery li img.reasons, #homeGallery li a.loadbottom").live('click',function(){
	jQuery('.reasonBox span').trigger('click');													
});

jQuery(".showForm").live('click',function(event){
	var v = jQuery(this);
	v.closest('form').slideUp(500,function(){jQuery("#"+v.attr('rel')).slideDown(500);});
	return false;
});

jQuery(".ajaxsubmit").live('click',function(){
	var v = jQuery(this);
		form = v.closest('form');
		options = { 
			beforeSubmit:  function(){
				v.closest('.affiliateTable').append('<div class="ajaxbusy" />');
			}, 
			success:  function(json){
				var data = json.response;
				if(json.parseData){ data = parseJSONdata(json.response);}
				if(json.errorno ==0){
					v.closest('.affiliateTable').find('.tableForm').html(json.data);
					v.closest('.affiliateTable').find(".ajaxbusy").remove();
					if(json.callback){jQuery(function(){eval(json.callback);});}
				}
				else {alert(json.error);v.closest('.affiliateTable').find(".ajaxbusy").remove();}
			},
			dataType: "json",
			error: function(){
				alert('Server Error');	
				v.closest('.affiliateTable').find(".ajaxbusy").remove();
			}
		}; 		
	$(form).ajaxSubmit(options);
	return false;	
});

jQuery(".adminpagination li a").live('click',function(){
	var v = jQuery(this);
	if(v.attr('href')){
		jQuery('.affiliateTable').append('<div class="ajaxbusy" />');
		jQuery.getJSON(v.attr('href'),function(json){
			if(json.errorno ==0){
				jQuery('.affiliateTable').find('.tableForm').html(json.data);
				jQuery('.affiliateTable').find(".ajaxbusy").remove();
				if(json.callback){jQuery(function(){eval(json.callback);});}
				if(json.pagination){jQuery('.adminpagination').html(json.pagination);}
			}
			else {alert(json.error);jQuery('.affiliateTable').find(".ajaxbusy").remove();}								   
		}).error(function(){alert('Error');})
	}
	return false;
});

jQuery(".reasonBox span, #shallowbox a.gohome").live('click',function(){
	jQuery('form :input').val("");
	jQuery('form').find('div.input').removeClass('error').find('span.error').remove();
	var v = jQuery(this),margin = jQuery(document).height()-194;
	if(jQuery("#shallowbox").is(":visible")){
		jQuery.scrollTo("#wrap",1000,function(){
			jQuery("#shallowbox").hide();
			jQuery(".shotableform").animate({top:40},1000,function(){jQuery("#footer .footer").show();});
			jQuery("#wrap").css({height:'auto','min-height':'100%'});
			if (jQuery('body').height() > 700) {var height = jQuery('body').height()}else {var height = 700;}
			jQuery("#wrap").animate({marginTop:0},1400,function(){
				jQuery(".reasonBox span").removeClass('opened').html('6 reasons you need A2');
				jQuery("#reasonContainer").hide();	
			});											 
		});
	}else {
		var topvalue = jQuery(document).height()-610;
		jQuery("#footer .footer").hide();
		jQuery("#reasonContainer").show();
		jQuery(".shotableform").animate({top:topvalue},1000);
		jQuery("#wrap").animate({marginTop:'-'+margin+'px'},1400,function(){
			jQuery.scrollTo("#wrap",500);
			jQuery("#wrap").css({'height':margin+194+'px','min-height':margin+194+'px'});
			jQuery(".reasonBox span").addClass('opened').html('Websites for Entertainers');
			jQuery("#shallowbox").show();
		});
	}
	return false;
});

jQuery(".affiliateslogin").live('click',function(){
	var v = jQuery(this);
	if(jQuery("#indexContainer").is(":visible")){
		jQuery(".reasonBox").slideUp(500);
		jQuery("#indexContainer").fadeOut(500,function(){
			jQuery('body').addClass('body-login');
			loadLogin('resized');
			jQuery("#loginContainer").fadeIn(500);
			v.text('Home');
		});
	}else {
		jQuery('body').removeClass('body-login');
		jQuery("#loginContainer").hide();
		jQuery(".reasonBox").slideDown(500);
		jQuery("#container").animate({'padding-top':'20px'},500,function(){
			jQuery("#indexContainer").fadeIn(500);
			v.text('Affiliates');
		});
	}
	return false;
});

function loadLogin(id){
	if(jQuery(window).height() < 500){
		jQuery(".body-login #container").css({'padding-top':'30px'});	
	}else {
		var height = jQuery(window).height()/2 -180;
		if(id){jQuery(".body-login #container").animate({'padding-top':height+'px'});}else {jQuery(".body-login #container").css({'padding-top':height+'px'});}
	}	
}

function affilateTabs(){
	jQuery(".affiliateTabs").tabs();		
}

function loadDatePicker(){
	jQuery(".datepicker").datepicker();
}

jQuery('.rn_customtooltip').live('mouseover', function(e){
	var t=$(this),
		rn_data = t.data('title');
	jQuery('#rn_CustomTooltip').remove();
	if(t.attr('title')!==''){
		t.data('title', t.attr('title')).attr('title', '');
	}else {
		rn_data = t.parent().find('.rn_tooltipdata').html();
	}
	var arrowbox = '<span class="tooltiparrow"/>';
	jQuery('<span id="rn_CustomTooltip" />').html(rn_data).css({
		left:t.offset().left+t.width()+20,
		top:t.offset().top- 35
	}).appendTo('body').append(arrowbox);
}).live('mouseout', function(){
	jQuery('#rn_CustomTooltip').remove();
});

jQuery("#header .head_news .controlbar").live('click',function(){
	var v = jQuery(this);
	if(v.hasClass('opened')){
		jQuery("#header .head_news li.news").animate({height:17},1000,function(){v.html('Expand for More &darr;');});	
		v.removeClass('opened');
	}else {
		jQuery("#header .head_news li.news").animate({height:10+jQuery("#header .head_news .newscontent").height()},1000,function(){v.html('Retract &uarr;');});
		v.addClass('opened');
	}
	
});
jQuery('.post-info a.share').live('click',function(){
	if(jQuery(this).hasClass("opened"))	{jQuery('div.sexy-bookmarks').slideUp(500);jQuery(this).removeClass('opened');}
	else {jQuery('div.sexy-bookmarks').slideDown(500);jQuery(this).addClass('opened');}
	return false;
});

/*
	click handler for SexyBookmarks
	Credit: Phong Thai Cao - http://www.JavaScriptBank.com
	Please keep this creadit when you use this code
*/
jQuery('.sexy-bookmarks a.external').click(function() {
	var url = encodeURIComponent(window.location.href), desc = '';
	if( jQuery('p.sexy-bookmarks-content').length ) {
		desc = encodeURIComponent(jQuery('p.sexy-bookmarks-content').text());
	}
	switch(this.parentNode.className) {
		case 'sexy-twittley':
			this.href += '?title=' + document.title + '&url=' + url + '&desc=' + desc + '&pcat=Internet&tags=';
			break;
		case 'sexy-digg':
			this.href += '?phase=2&title=' + document.title + '&url=' + url + '&desc=' + desc;
			break;
		case 'sexy-twitter':
			this.href += '?status=RT+@your_twitter_id:+' + document.title + '+-+' + url;
			break;
		case 'sexy-scriptstyle':
			this.href += '?title=' + document.title + '&url=' + url;
			break;
		case 'sexy-reddit':
			this.href += '?title=' + document.title + '&url=' + url;
			break;
		case 'sexy-delicious':
			this.href += '?title=' + document.title + '&url=' + url;
			break;
		case 'sexy-stumbleupon':
			this.href += '?title=' + document.title + '&url=' + url;
			break;
		case 'sexy-mixx':
			this.href += '?title=' + document.title + '&page_url=' + url + '&desc=' + desc;
			break;
		case 'sexy-technorati':
			this.href += '?add=' + url;
			break;
		case 'sexy-blinklist':
			this.href += '?Action=Blink/addblink.php&Title=' + document.title + '&Url=' + url;
			break;
		case 'sexy-diigo':
			this.href += '?title=' + document.title + '&url=' + url + '&desc=' + desc;
			break;
		case 'sexy-yahoobuzz':
			this.href += '?submitHeadline=' + document.title + '&submitUrl=' + url + '&submitSummary=' + desc + '&submitCategory=science&submitAssetType=text';
			break;
		case 'sexy-myspace':
			this.href += '?t=' + document.title + '&u=' + url;
			break;
		case 'sexy-facebook':
			this.href += '?t=' + document.title + '&u=' + url;
			break;
		case 'sexy-designfloat':
			this.href += '?title=' + document.title + '&url=' + url;
			break;
		case 'sexy-devmarks':
			this.href += '?posttitle=' + document.title + '&posturl=' + url + '&posttext=' + desc;
			break;
		case 'sexy-newsvine':
			this.href += '?h=' + document.title + '&u=' + url;
			break;
		case 'sexy-google':
			this.href += '?op=add&title=' + document.title + '&bkmk=' + url;
			break;
	}
})

jQuery(document).ready(function()
{
	if(jQuery(".affiliateTabs").length)
	{
		 affilateTabs();
	}	
	if(jQuery(".datepicker").length)
	{
		 loadDatePicker();
	}	
});
