var is_ipad = ( navigator.platform == "iPad" );
var is_iphone = ( navigator.platform == "iPhone" );

Cufon.replace('h1');
Cufon.replace('h2');
Cufon.replace('h3');
Cufon.replace('h4');
Cufon.replace('#breadcrumbnavlist');

jQuery.noConflict();
jQuery(document).ready(function() {
    
    p3_widgets();
    p3_sidebar_drawers();
    jQuery('#box_other').click(function () {
        if (jQuery('#box_other').is(':checked')) {
            jQuery('#text_other_box').show();
        } else {
            jQuery('#text_other_box').hide();
        }
    });
    jQuery("#order_form").validate({
        errorElement: "p"
    });
    jQuery('#box_add_photo').click(function () {
        jQuery('#box_5_photo').attr("checked",true);
        jQuery('#additional').val('');
        jQuery('#additional_box').show();
        total_order();
    });
    jQuery('#box_1_photo').click(function () {
        if (jQuery('#box_add_photo').is(':checked')) {
            jQuery('#additional_box').hide();
        }
        jQuery('#box_add_photo').attr("checked",false);
        jQuery('#additional').val('');
        total_order();
    });
    jQuery('#box_2_photo').click(function () {
        if (jQuery('#box_add_photo').is(':checked')) {
            jQuery('#additional_box').hide();
        }
        jQuery('#box_add_photo').attr("checked",false);
        jQuery('#additional').val('');
        total_order();
    });
    jQuery('#box_3_photo').click(function () {
        if (jQuery('#box_add_photo').is(':checked')) {
            jQuery('#additional_box').hide();
        }
        jQuery('#box_add_photo').attr("checked",false);
        jQuery('#additional').val('');
        total_order();
    });
    jQuery('#box_4_photo').click(function () {
        if (jQuery('#box_add_photo').is(':checked')) {
            jQuery('#additional_box').hide();
        }
        jQuery('#box_add_photo').attr("checked",false);
        jQuery('#additional').val('');
        total_order();
    });
    jQuery('#box_5_photo').click(function () {
        if (jQuery('#box_add_photo').is(':checked')) {
            jQuery('#additional_box').hide();
        }
        jQuery('#box_add_photo').attr("checked",false);
        jQuery('#additional').val('');
        total_order();
    });
    jQuery('#additional').blur(function () {
        total_order();
    });

    /*---------------			FANCYBOX			---------------*/

    jQuery('#gallery .gallery_tab ul.photos_list li a').fancybox({
        'overlayColor' : '#000',
        'overlayOpacity' : 0.65,
        'titlePosition' : 'over'
    });
    jQuery('a.fancybox').fancybox({
        'overlayColor' : '#000',
        'overlayOpacity' : 0.65,
        'titlePosition' : 'over'
    });
	
    /*---------------			IE6 NAVIGATION FIX			---------------*/

    jQuery('#nav li').hover(function() {
        jQuery(this).addClass('hover');
    }, function() {
        jQuery(this).removeClass('hover');
    });

    /*---------------			CONTACT FORM			---------------*/

    jQuery('#submit').click(function() {
        var name = jQuery('#name').val();
        var email = jQuery('#email').val();
        var message = jQuery('#message').val();
        var ip = jQuery('#ip').val();
        var heard = jQuery('#heard').val();
        var other = jQuery('#other').val();
        var i = 1;
        jQuery('#response').append('<img src="http://www.sevenhillsphotography.com/theme/SevenHillsPhotography/images/ajax-loader.gif" alt="Loading." /> Please Wait...');
        jQuery.ajax({
            type: 'post',
            url: 'http://www.sevenhillsphotography.com/theme/SevenHillsPhotography/php/sending.php',
            dataType: 'json',
            data: 'name=' + name + '&email=' + email + '&message=' + message + '&ip=' + ip + '&heard=' + heard + '&other=' + other,
            success: function(results) {
                jQuery('#response').html(results.response).fadeIn();
            }
        });
        return false;
    });

    /*---------------			ORDER FORM			---------------*/

    //    jQuery('#order').click(function() {
    //        var name = jQuery('#name').val();
    //        var email = jQuery('#email').val();
    //        var ip = jQuery('#ip').val();
    //        var phone = jQuery('#phone').val();
    //        var address = jQuery('#address').val();
    //        var photos = jQuery('input[name=photos]:checked').val();
    //        var options = jQuery('input:checkbox').serializeArray();
    //        jQuery('#response').append('<img src="http://www.sevenhillsphotography.com/theme/SevenHillsPhotography/images/ajax-loader.gif" alt="Loading." /> Please Wait...');
    //        jQuery.ajax({
    //            type: 'post',
    //            url: 'http://www.sevenhillsphotography.com/theme/SevenHillsPhotography/php/neworder.php',
    //            dataType: 'json',
    //            data: 'name=' + name + '&email=' + email + '&phone=' + phone + '&address=' + address + '&ip=' + ip + '&photos=' + photos + '&options=' + options,
    //            success: function(results) {
    //                jQuery('#response').html(results.response).fadeIn();
    //            }
    //        });
    //        return false;
    //    });

    /*---------------			BEGINS GALLERY SCRIPTS			---------------*/

    if (jQuery('body').hasClass('gallery')) {
	
        /*---------------			GALLERY CATEGORIES			---------------*/

        jQuery('#gallery_nav').show();
        jQuery('#gallery .gallery_tab').show();
        jQuery('#gallery').children('div').hide();
        var hash = window.location.hash.substr(1);
        if (!hash)
            hash = 1;
        jQuery('#gallery #tab-' + hash).show();
        jQuery('#gallery_nav li:nth-child(' + hash + ')').addClass('current');
        jQuery('#gallery .gallery_stats').show();
        jQuery('.paginate ul.photos_list').hide();
        jQuery('#tab-' + hash + ' .photos_list:first').show();
        jQuery('#tab-' + hash + ' .pagination li:first').addClass('current');
	
        jQuery('#gallery_nav li a').click(function(){
            hash = jQuery(this).parent('li').index() + 1;
            window.location.hash = hash;
            jQuery('.paginate ul.photos_list').hide();
            jQuery('#tab-' + hash + ' .photos_list:first').show();
            jQuery('#tab-' + hash + ' .pagination li:first').addClass('current');
            jQuery('.pagination li').removeClass('current');
            jQuery('#tab-' + hash + ' .pagination li:first').addClass('current');
            jQuery('#gallery_nav li').removeClass('current');
            jQuery(this).parent('li').addClass('current');
            href = jQuery(this).attr('href').split('#')[1];
            jQuery('#gallery').children('div:not(#' + href + ')').hide();
            if (jQuery.browser.msie && jQuery.browser.version.substr(0,3) == "6.0") {
                jQuery('#' + href).show();
            } else jQuery('#' + href).fadeIn();
            return false;
        });

        /*---------------			GALLERY PAGINATION			---------------*/
	
        jQuery('.pagination li a').click(function() {
            var t = jQuery(this).text();
            jQuery('.pagination li').removeClass('current');
            jQuery(this).parent('li').addClass('current');
            jQuery('.paginate ul.photos_list').hide();
            jQuery('#tab-' + hash + ' ul.photos_list:nth-child(' + t + ')').fadeIn();
            return false;
        });
	
    }
	
});

function p3_sidebar_drawers() {
    var drawer_padding = 25;

    // set initial opacity and height of drawers
    jQuery('.drawer_content, .tab').css('opacity', 0.85);
    jQuery('.drawer_content').css('height', (jQuery(window).height() - 25) + 'px' ); 

    // function-scoped timeout var object
    var p3_close_drawer_timeout = new Object();

    // instrument each drawer
    jQuery('.drawer').each(function(){
        var drawer = jQuery(this);
        var id     = drawer.attr('id');
        var tab    = jQuery('.tab', this);
        var width  = parseInt( jQuery('.drawer_content', drawer).css( 'width' ) ) + 25;
        var speed  = parseInt( width * 0.75 );

        // show the drawer on tab mouseover
        tab.mouseover(function(){
            drawer
            .css( 'z-index', '10000' )
            .animate( {
                left:'0px'
            }, speed, 'swing' )
            .addClass( 'open' );
        });

        // handle iphone/ipad show/hides
        if ( is_ipad || is_iphone ) {
            jQuery('#inner-wrap').unbind('touchstart').bind('touchstart',function(){
                drawer.mouseleave();
            });
            tab.bind('touchstart',function(){
                if ( drawer.hasClass('open') ) drawer.trigger('mouseleave');
            });
        }

        // set the timeout to close drawer on mouseleave
        drawer.mouseleave(function(){
            var delay = ( is_ipad || is_iphone ) ? 1 : 1000;
            clearTimeout( p3_close_drawer_timeout[id] );
            p3_close_drawer_timeout[id] = setTimeout( function(){
                drawer.animate( {
                    left:'-' + width + 'px'
                }, speed, 'swing', function(){
                    drawer.css( 'z-index', '5000' ).removeClass('open');
                } );
            }, delay );
        });

        // restart the close drawer timout on mouseenter
        drawer.mouseenter(function(){
            clearTimeout( p3_close_drawer_timeout[id] );
        });
    });	
}

function p3_widgets() {
		
}

function new_order() {
    var name = jQuery('#name').val();
    var email = jQuery('#email').val();
    var ip = jQuery('#ip').val();
    var phone = jQuery('#phone').val();
    var address = jQuery('#address').val();
    var photos = jQuery('input[name=photos]:checked').val();
    var photos_add = jQuery('input[name=photos_add]:checked').val();
    var options = jQuery('input[name=options]').serialize();
    var options2 = options.replace('&',';').replace('&',';').replace('&',';').replace('&',';').replace('&',';');
    var text_other = jQuery('#text_other').val();
    var additional = jQuery('#additional').val();
    jQuery('#response').append('<img src="http://www.sevenhillsphotography.com/theme/SevenHillsPhotography/images/ajax-loader.gif" alt="Loading." /> Please Wait...');
    jQuery.ajax({
        type: 'post',
        url: 'http://www.sevenhillsphotography.com/theme/SevenHillsPhotography/php/neworder.php',
        dataType: 'json',
        data: 'name=' + name + '&email=' + email + '&phone=' + phone + '&address=' + address + '&ip=' + ip + '&photos=' + photos + '&options=' + options2 + '&text_other=' + text_other + '&additional=' + additional + '&photos_add=' + photos_add,
        success: function(results) {
            jQuery('#response').html(results.response).fadeIn();
        }
    });
    return false;
}

function total_order() {
    var total = 0;
    var photos = jQuery('input[name=photos]:checked').val();
    var additional = jQuery('#additional').val();
    if (photos == '1_photo') {
        total = total+30;
    } else if (photos == '2_photo') {
        total = total+55;
    } else if (photos == '3_photo') {
        total = total+75;
    } else if (photos == '4_photo') {
        total = total+90;
    } else if (photos == '5_photo') {
        total = total+100;
    }
    if (additional.length > 0) {
        total = (total+(additional*10));
    }
    jQuery('#order_total').val(total);
}
