﻿var pipenoTextInputs = new Array();
var pipenoCheckBoxes = new Array();
var pipenoComboBox = new Array();
var pipenoTexatreaItems = new Array();

$(document).ready(function () {
	/* HANDLE THE ORANDA FONT IMPLEMENTATION */
	Cufon.replace('.button .content', { fontFamily: 'Oranda' });
	
	Cufon.replace("#navigation .active a", { "fontFamily": 'Oranda' });
	Cufon.replace("#navigation .homepage-active a", { "fontFamily": 'Oranda' });
	Cufon.replace("#navigation a", { "fontFamily": 'Oranda', "hover": { "color": "#05f063" } });
	
	Cufon.replace("h1", { "fontFamily": 'Oranda' });
	Cufon.replace("h2", { "fontFamily": 'Oranda' });
	Cufon.replace(".getapp", { "fontFamily": 'Oranda' });
	Cufon.replace(".homepage .feeds .btn", { "fontFamily": 'Oranda' });
	Cufon.replace(".appstore", { "fontFamily": 'Oranda' });
	/* END HANDLE THE ORANDA FONT IMPLEMENTATION */
	
    texatreaItems = $('.textarea');
    $(texatreaItems).each(function () {
        pipenoTexatreaItems[this.id] = new PipenoUI.RoundedSimpleInputText(this, "", "", 200, { isTextarea: "true", inputName: this.id });
    });

    inputs = $('.input');
    $(inputs).each(function () {
        if ($(this).hasClass('password')) {
            pipenoTextInputs[this.id] = new PipenoUI.RoundedSimpleInputText(this, "", "", 200, { type: "password", inputName: this.id });
        } else {
            pipenoTextInputs[this.id] = new PipenoUI.RoundedSimpleInputText(this, "", "", 200, { type: "text", inputName: this.id });
        }
    });

    checkBoxes = $('.checkbox');
    $(checkBoxes).each(function () {
        pipenoCheckBoxes[this.id] = new PipenoUI.CheckBox(this, { checked: false, enabled: true });
    });

    var gender = $('.gender');
    var pipenoGender = new PipenoUI.RadioGroup(gender, { option1: 'value1', option2: 'value 2' });

    comboBox = $('.pipeno-combobox');
    $(comboBox).each(function () {
        pipenoComboBox[this.id] = new PipenoUI.ComboBox(this);
    });

    $('#location input').focus(function () {
        $('#suggestions-location').show();
    });

    $('#location input').blur(function () {
        $('#suggestions-location').hide();
    });

    /* HANDLE THE SUBSCRIBE BOX */
    var subscribeBox = $('#subscribe-popup');
    var openButton = $('#open-subscribe-popup');
    $(document).click(function () {
        $(subscribeBox).hide();
    });
    $(subscribeBox).click(function (e) {
        e.stopPropagation();
    });
    $(openButton).click(function (e) {
        if (!$(subscribeBox).is(':visible')) {
            $(subscribeBox).find('.input').removeClass('error');
            $(subscribeBox).find('.input').show();
            $(subscribeBox).find('.button-small').show();
            $(subscribeBox).find('.message').remove();
        }
        $(subscribeBox).toggle();
        e.stopPropagation();
    });
    $(subscribeBox).find('.button-small').click(function () {
        var val = pipenoTextInputs['subscriber-address'].Value();
        if (echeck(val)) {
            $.ajax({
                url: '/SaveSubscriber',
                data: { 'Email': val },
                type: 'POST'
            });
            $(subscribeBox).find('.input').hide();
            $(subscribeBox).find('.button-small').hide();
            $(subscribeBox).find('.middle').append($('<div/>', { 'text': 'Thank you!', 'class': 'message' }));
            pipenoTextInputs['subscriber-address'].Value("");
        }
        else {
            $(subscribeBox).find('.input').addClass('error');
        }
    });
    /* END HANDLE THE SUBSCRIBE BOX */
    
    $(".target-blank").click(function(){
    	window.open($(this).attr("href"), "_newtab");
    	return false;
    });
});

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)

	if (str.length == 0) {return false; }
    if (str.indexOf(at)==-1){ return false; }
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ return false; }
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ return false;}
	if (str.indexOf(at,(lat+1))!=-1) { return false; }
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) { return false; }
	if (str.indexOf(dot,(lat+2))==-1) {return false; }
	if (str.indexOf(" ")!=-1) { return false; }
	return true;
}
