var Fathersday = {
    lastNav: 'featured',

    loadGiftIdeas: function(linkname, divId) {
        if (Fathersday.lastNav != '') {
            $('#'+this.lastNav).toggleClass('currentPage', false);
        }
        $.ajax({
                cache: true,
                success: function (data, textStatus) {
                    $('#'+divId).html(data);
                    $('#'+linkname).toggleClass('currentPage', true);
                    Fathersday.lastNav = linkname;
                    Fathersday.updateGoogleSearchAds(linkname + ' Gift Ideas');
                },
                data: { linkname: linkname },
                dataType: 'text',
                timeout: 4000,
                type: 'get',
                url: '/giftideas/' + linkname + '/'
        });
    },
    
    showDiscount: function(id) {
        var u = '/discount/' + id;
        win = window.open(u,'win','menubar,resizable,height=480,width=640,left=400,top=0,scrollbars=yes,location=no,toolbar=yes');
        win.blur();
        window.focus();
        if(win.closed) {
            win = window.open(u,'win','menubar,resizable,height=480,width=640,left=400,top=0,scrollbars=yes,location=no,toolbar=yes');
            win.blur();
            window.focus();
        } else {
            win.location.href = u;
            win.blur();
            window.focus();
        }
        return true;
    },
    
    updateGoogleSearchAds: function(searchTerm) {
        if (google) {
            searchTerm = searchTerm.replace(/-/g, ' ');
            dynamicAd = new google.ads.search.Ad({
                'pubId' : 'pub-5415304143709494',
                'query' : searchTerm,
                'container' : 'adcontainer', 
                'format' : 'wide',
                'number' : 2,
                'channel': '7760670376',
                //'colorBorder': '#ffffff',
                'colorBackground': '#ffffff'
                //'colorDomainLink': '#0000ff',
                //'colorTitleLink': '#000000',
                //'colorText': '#000000'
            });
        }
    }


};

$(document).ready(function() { 
    $('#newsletter').ajaxForm({
        beforeSubmit: function(formData, jqForm, options) { 
            var form = jqForm[0];
            var ret = true; 
            
            if ($('#NewsletterEmail').val() == '') {
                $("#newsletterStatus").html("Please enter your email address.");
                ret = false;
            }
            return(ret);
        },
        //cache: false,
        success: function (data, textStatus) {
            if (data.result == 'error') {
                $("#newsletterStatus").html(data.errorMessage);
                return false;
            }
            
            $('#newsletter').fadeOut(400, function() {
                $(this).html(data.message);
                $(this).fadeIn(400);
            });
        },
        
        //data: pars,
        dataType: 'json',
        //timeout: 4000,
        //type: 'post',
        url: '/newsletter/submit/'
    });

    $('#CommentForm').ajaxForm({
        beforeSubmit: function(formData, jqForm, options) { 
            var form = jqForm[0];
            var ret = true; 
            var errors = [];
            
            if ($('#ContactName').val() == '') {
                errors.push("Please enter your name.");
            }
            if ($('#ContactEmail').val() == '') {
                errors.push("Please enter your email address.");
            }
            if ($('#ContactComment').val() == '') {
                errors.push("Please enter a comment.");
            }

            if (errors.length) {
                $("#ContactStatus").html(errors.join("<br/>"));
                ret = false;
            }
            return(ret);
        },
        //cache: false,
        success: function (data, textStatus) {
            if (data.result == 'error') {
                $("#ContactStatus").html(data.errorMessage);
                return false;
            }
            
            $('#CommentForm').fadeOut(400, function() {
                $(this).html(data.message);
                $(this).fadeIn(400);
            });
        },
        
        //data: pars,
        dataType: 'json',
        //timeout: 4000,
        //type: 'post',
        url: '/contact/submit/'
    });

    
    
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------
    // ONLOAD.JS
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------
    // --------------------------------------------------------------------
    
    
    // image hover effects  
    // -------------------------------------------------------------------
    $("a.img").hover( function () {
        if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) <= 8) {
            $(this).stop(false, true).toggleClass('imgHover');
        } else {
            $(this).stop(false, true).toggleClass('imgHover', 200);
        }
    });
    
    
            
    // Text and password input styling
    // -------------------------------------------------------------------
    
    // This should be in the CSS file but IE 6 will ignore it.
    // If you have an input you don't want styles, add the class "noStyle"

    $("input[type='text']:not(.noStyle), input[type='password']:not(.noStyle)").each(function(){
        $(this).addClass('textInput');
    });


    // 3D ribbon scroll triggers
    // -------------------------------------------------------------------
    // trigger when scrolling occurs
    $(window).scroll(function () {
        ribbonPosition();
    });
    // run once on load
    ribbonPosition();   

                           
    // input lable replacement
    // -------------------------------------------------------------------
    $("label.overlabel").overlabel();

    // apply custom search input functions
    // -------------------------------------------------------------------
    searchInputEffect();
        
    // apply custom button styles
    // -------------------------------------------------------------------
    buttonStyles();
    
    // CSS Rounded Corners (not for IE)
    // -------------------------------------------------------------------
    if (!jQuery.browser.msie) {
        $("a.img, div.img, .textInput, input[type='text'], input[type='password'], textarea").addClass('rounded');  // items to add rounded class
        roundCorners(); // execute it!
    }

    
    
});


//======================================================================
//
//  Design functions
//
// ======================================================================


    
// Modal after load functions
// -------------------------------------------------------------------

function modalStart() {
    // apply font replacement
    Cufon.replace('.fancy_title > div');
    
    // updated styles
    $('#fancy_inner').addClass('rounded');
    roundCorners();
}


// Ribbon scrolling effects 
// -------------------------------------------------------------------

function ribbonPosition() {
    var t = $(window).scrollTop();
    var h = $(window).height();
    
    // offset - increase or decrease size of middle (set to zero to disable)
    var offset = $(window).height() / 25;   
    
    var zoneSize = $(window).height() / 3;
    var zoneOne = t + zoneSize + offset;
    var zoneTwo = t + zoneSize * 2 - offset;
        
    $(".ribbon .wrapAround").each( function() {
        var obj = $(this);
        var objH = obj.height();
        var offset = obj.offset();
        if (offset.top + objH <= zoneOne) {
            $(this).css('background-position','0 0');
        } else if (offset.top >= zoneTwo) {
            $(this).css('background-position','0 -104px');
        } else {
            $(this).css('background-position','0 -52px');
        }
    }); 
}



// Search input - custom effects for mouse over and focus.
// -------------------------------------------------------------------

function searchInputEffect() {

    var searchFocus = false,
        searchHover = false,
        searchCtnr = $('#Search');
        searchInput = $('#SearchInput'),
        searchSubmit = $('#SearchSubmit');
    // Search input - mouse events
    searchCtnr.hover(
        function () {   // mouseover
            if (!searchFocus) $(this).addClass('searchHover');
            searchHover = true; }, 
        function () {   // mouseout
            if (!searchFocus) $(this).removeClass('searchHover');
            searchHover = false;
    }).mousedown( function() {
        if (!searchFocus) $(this).removeClass('searchHover').addClass('searchActive');
    }).mouseup( function() {
        searchInput.focus();
        searchSubmit.show();
        searchFocus = true;
    });
    // set focus/blur events
    searchInput.blur( function() {
        if (!searchHover) {
            searchCtnr.removeClass('searchActive');
            searchSubmit.hide();
            searchFocus = false;
        }
    });
}



// button styling function
// -------------------------------------------------------------------

function buttonStyles() {
    // Button styles
    
    // This will style buttons to match the theme. If you don't want a button
    // styled, give it the class "noStyle" and it will be skipped.
    $("button:not(:has(span),.noStyle), input[type='submit']:not(.noStyle), input[type='button']:not(.noStyle)").each(function(){
        var b = $(this),
            tt = b.html() || b.val();
        
        // convert submit inputs into buttons
        if (!b.html()) {
            b = ($(this).attr('type') == 'submit') ? $('<button type="submit">') : $('<button>');
            b.insertAfter(this).addClass(this.className).attr('id',this.id);
            $(this).remove();   // remove input
        }
        b.text('').addClass('btn').append($('<span>').html(tt));    // rebuilds the button
    });
    
    // Get all styled buttons
    var styledButtons = $('.btn');
    
    // Fix minor problem with Mozilla and WebKit rendering (can also be done adding this to CSS, 
    // button::-moz-focus-inner {border: none;}
    // @media screen and (-webkit-min-device-pixel-ratio:0) { button span {margin-top: -1px;} }
    if (jQuery.browser.mozilla || jQuery.browser.webkit) {
//      styledButtons.children("span").css("margin-top", "-1px");
    }
    
    // Button hover class (IE 6 needs this)
    styledButtons.hover(
        function(){ $(this).addClass('submitBtnHover'); },      // mouseover
        function(){ $(this).removeClass('submitBtnHover'); }    // mouseout
    );
}

// Rounded corner styles
// -------------------------------------------------------------------

function roundCorners() {
    $('.rounded, .ui-corner-all').css({
        '-moz-border-radius': '4px',
        '-webkit-border-radius': '4px',
        'border-radius': '4px'
    });
}



// Font replacement
// -------------------------------------------------------------------

Cufon.replace('h1, h2, h3, h4, h5, .featuredText a.heading, .fancy_title div, #Footer b, .header, .one-third strong, .portfolio-item b');

