
$(document).ready(function() {
	
	setupBlogHovers();
	setupBlogComments();
	setupCombo();
	setupCombo2();
	setupForm();
	setupSubscribe();
	
	$("#rhs .get_email_alerts").click(function() {
		openSubscribe();
	});
	
	if(ie7) {
		$('#rhs .fb').html('<iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2FShoreThingNYE&amp;width=300&amp;colorscheme=light&amp;show_faces=true&amp;stream=false&amp;header=false&amp;height=190" scrolling="no" frameborder="0" style="" allowTransparency="true"></iframe>');
	}
	else {
		$('#rhs .fb').html('<iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2FShoreThingNYE&amp;width=300&amp;colorscheme=light&amp;show_faces=true&amp;stream=false&amp;header=false&amp;height=182" scrolling="no" frameborder="0" style="" allowTransparency="true"></iframe>');
	}
	$('body').append('<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4db900b947938aff"></script>');
	
	ytimagesplayer_run();
});

function setupBlogHovers() {
	$("#main .blogs .blog_item").each(function() {
		$(this).find('.rollover').css('opacity', 0.8);
		
		var $left = $(this).find('.left');
		
		$left.mouseenter(function() {
			$(this).find('.rollover').stop().clearQueue().animate({bottom: '1px'});
		}).mouseleave(function() {
			$(this).find('.rollover').stop().clearQueue().animate({bottom: '-52px'});
		});
	});
}

function setupBlogComments() {
	$("#main .blogs .blog .social .comment").each(function() {
		$(this).click(function() {
			var $comments = $(this).parent().parent().find('.comments');
			if($comments.css('display') == 'none') {
				$comments.show();
				var h = $comments.height();
				$comments.hide();
				$comments.height(0);
				$comments.show();
				$comments.animate({height: h + 'px'});
			}
			return false;
		});
	});
}

function setupCombo() {
	$("#main .contactus .row .field select").each(function() {
		$field = $(this).parent();
		var def = $field.find('input[type=hidden]').val();
		var html = '<div class="arrow"></div><div class="selected default">' + def + '</div><div class="overlay"></div><div class="items">';
		
		$field.find('select option').each(function() {
			if($(this).text() != "") {
				html += '<div class="item">' + $(this).text() + '</div>';
			}
		});
		
		html += '</div>';
		$field.append(html);
		
		$field.find('.item').mouseover(function() {
			$(this).addClass('active');
		}).mouseout(function() {
			$(this).removeClass('active');
		}).click(function() {
			$("#main .contactus .row .field").show();
			$(this).parent().parent().find('.selected').html($(this).text()).removeClass('default');
			$(this).parent().parent().find('select').val($(this).text());
			$(this).parent().parent().find('.overlay').hide();
			$(this).parent().parent().find('.items').hide();
			return false;
		});
		
		$field.click(function() {
			if($(this).find('.items').css('display') == 'none') {
				$("#main .contactus .row .field").hide();
				$("#main .contactus .row .field select[name=state]").parents('.field').show();
				$(this).show();
				$(this).find('.overlay').show();
				$(this).find('.items').show();
			}
			else {
				$("#main .contactus .row .field").show();
				$(this).find('.overlay').hide();
				$(this).find('.items').hide();
			}
		});
		
		$field.find('.overlay').click(function() {
			$("#main .contactus .row .field").show();
			$(this).parent().find('.overlay').hide();
			$(this).parent().find('.items').hide();
			return false;
		});
	});
	$("#main .contactus .row .field input[type=text]").each(function() {
		$(this).data('def', $(this).val());
		$(this).focus(function() {
			if($(this).val() == $(this).data('def')) {
				$(this).val('');
				$(this).removeClass('default');
			}
		}).blur(function() {
			if($(this).val() == '') {
				$(this).val($(this).data('def'));
				$(this).addClass('default');
			}
		});
	});
}

function setupCombo2() {
	$("#popup_subscribe .body .row .field select").each(function() {
		$field = $(this).parent();
		var def = $field.find('input[type=hidden]').val();
		var html = '<div class="arrow"></div><div class="selected default">' + def + '</div><div class="overlay"></div><div class="items">';
		
		$field.find('select option').each(function() {
			if($(this).text() != "") {
				html += '<div class="item">' + $(this).text() + '</div>';
			}
		});
		
		html += '</div>';
		$field.append(html);
		
		$field.find('.item').mouseover(function() {
			$(this).addClass('active');
		}).mouseout(function() {
			$(this).removeClass('active');
		}).click(function() {
			$("#popup_subscribe .body .row .field").show();
			$(this).parent().parent().find('.selected').html($(this).text()).removeClass('default');
			$(this).parent().parent().find('select').val($(this).text());
			$(this).parent().parent().find('.overlay').hide();
			$(this).parent().parent().find('.items').hide();
			return false;
		});
		
		$field.click(function() {
			if($(this).find('.items').css('display') == 'none') {
				$("#popup_subscribe .body .row .field").hide();
				$("#popup_subscribe .body .row .field input[name=firstname]").parents('.field').show();
				$("#popup_subscribe .body .row .field input[name=secondname]").parents('.field').show();
				$("#popup_subscribe .body .row .field input[name=email]").parents('.field').show();
				$(this).show();
				$(this).find('.overlay').show();
				$(this).find('.items').show();
			}
			else {
				$("#popup_subscribe .body .row .field").show();
				$(this).find('.overlay').hide();
				$(this).find('.items').hide();
			}
		});
		
		$field.find('.overlay').click(function() {
			$("#popup_subscribe .body .row .field").show();
			$(this).parent().find('.overlay').hide();
			$(this).parent().find('.items').hide();
			return false;
		});
	});
	$("#popup_subscribe .body .row .field input[type=text]").each(function() {
		$(this).data('def', $(this).val());
		$(this).focus(function() {
			if($(this).val() == $(this).data('def')) {
				$(this).val('');
				$(this).removeClass('default');
			}
		}).blur(function() {
			if($(this).val() == '') {
				$(this).val($(this).data('def'));
				$(this).addClass('default');
			}
		});
	});
}

function setupForm() {
	$("#main .contactus .submit").click(function() {
		
		openLoader();
		
		$("#main .contactus .field").removeClass('error');
		$("#main .contactus textarea").removeClass('error');
		
		var post = {
			state: $("#main .contactus select[name=state]").val(),
			department: $("#main .contactus select[name=department]").val(),
			fullname: $("#main .contactus input[name=fullname]").val(),
			email: $("#main .contactus input[name=email]").val(),
			phone: $("#main .contactus input[name=phone]").val(),
			message: $("#main .contactus textarea[name=message]").val()
		};
		
		$.ajax({
	        dataType:   "json",
	        url:        _SEND_FORM_URL,
	        data:		post,
	        type:		"POST",
	        cache:      false,
	        success:    function(data) {
			
				$("#main .contactus .field").removeClass('error');
			
		    	if(data && data.status && data.status == 'success') {
		    		openContactUsConfirmation();
		    		$("#main .contactus select[name=state]").val('');
		    		$("#main .contactus select[name=state]").parents('.field').find('.selected').html('Select your state...').addClass('default');
		    		$("#main .contactus select[name=department]").val('');
		    		$("#main .contactus select[name=department]").parents('.field').find('.selected').html('Select department...').addClass('default');
		    		$("#main .contactus input[name=fullname]").val($("#main .contactus input[name=fullname]").data('def')).addClass('default');
		    		$("#main .contactus input[name=email]").val($("#main .contactus input[name=email]").data('def')).addClass('default');
		    		$("#main .contactus input[name=phone]").val($("#main .contactus input[name=phone]").data('def')).addClass('default');
		    		$("#main .contactus textarea[name=message]").val('');
		    	}
		    	else {
		    		if(data && data.errors) {
		    			$.each(data.errors, function(k, v) {
		    				if(v === true) {
	    						$("#main .contactus textarea[name=" + k + "]").addClass('error');
	    						$("#main .contactus input[name=" + k + "]").parents('.field').addClass('error');
	    						$("#main .contactus select[name=" + k + "]").parents('.field').addClass('error');
		    				}
		    			});
		    		}
		    	}
		    	
		    	closeLoader();
	        }
	    });
	});
	
	return false;
}

function setupSubscribe() {
	
	$("#popup_subscribe .body .submit").click(function() {
		
		openLoader();
		
		$("#popup_subscribe .body .row .field").removeClass('error');
		
		var post = {
			state: $("#popup_subscribe select[name=state]").val(),
			firstname: $("#popup_subscribe input[name=firstname]").val(),
			email: $("#popup_subscribe input[name=email]").val(),
			secondname: $("#popup_subscribe input[name=secondname]").val()
		};
		
		$.ajax({
	        dataType:   "json",
	        url:        _SEND_EXACT_URL,
	        data:		post,
	        type:		"POST",
	        cache:      false,
	        error:		function() {
				openSubscribeError();
				clearSubscribe();
				closeLoader();
			},
	        success:    function(data) {
			
				$("#popup_subscribe .body .row .field").removeClass('error');
			
		    	if(data && data.status && data.status == 'success') {
		    		openSubscribeConfirmation();
		    		clearSubscribe();
		    	}
		    	else if(data && data.status && data.status == 'error' && data.errors) {
	    			$.each(data.errors, function(k, v) {
	    				if(v === true) {
    						$("#popup_subscribe input[name=" + k + "]").parents('.field').addClass('error');
    						$("#popup_subscribe select[name=" + k + "]").parents('.field').addClass('error');
	    				}
	    			});
		    	}
		    	else {
		    		openSubscribeError();
		    		clearSubscribe();
		    	}
		    	
		    	closeLoader();
	        }
	    });
	});
}

function openSubscribeError() {
	$("#popup_subscribe .body.error").show();
	$("#popup_subscribe .body.confirmation").hide();
	$("#popup_subscribe .body.subscription").hide();
}

function openSubscribeConfirmation() {
	$("#popup_subscribe .body.error").hide();
	$("#popup_subscribe .body.confirmation").show();
	$("#popup_subscribe .body.subscription").hide();
}

function clearSubscribe() {
	$("#popup_subscribe select[name=state]").val('');
	$("#popup_subscribe select[name=state]").parents('.field').find('.selected').html('Select your state...').addClass('default');
	$("#popup_subscribe input[name=firstname]").val($("#popup_subscribe input[name=firstname]").data('def')).addClass('default');
	$("#popup_subscribe input[name=email]").val($("#popup_subscribe input[name=email]").data('def')).addClass('default');
	$("#popup_subscribe input[name=secondname]").val($("#popup_subscribe input[name=secondname]").data('def')).addClass('default');
}

function openContactUsConfirmation() {
	$("#popup_contactus_confirmation .close").unbind().click(function() {
		$("#popup_contactus_confirmation").hide();
	});
	$("#popup_contactus_confirmation .overlay").css('opacity', 0.7);
	$("#popup_contactus_confirmation").show();
}

function openSubscribe() {
	$("#popup_subscribe .close").unbind().click(function() {
		$("#popup_subscribe").hide();
		$("#popup_subscribe .body.error").hide();
		$("#popup_subscribe .body.confirmation").hide();
		$("#popup_subscribe .body.subscription").show();
		
	});
	$("#popup_subscribe .overlay").css('opacity', 0.7);
	$("#popup_subscribe").show();
}

function openLoader() {
	//$("#loader").css('opacity', 0.7).show();
}

function closeLoader() {
	//$("#loader").hide();
}

