$(document).ready( function() {
    
    setOrientation();
    setPosition();
    
    var supportsOrientationChange = "onorientationchange" in window, orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";
    window.addEventListener(orientationEvent, setOrientation, false);
    
    // add input values to links within form
    if($('#actionform').length) {
        $('#actionform').find('a[href]').each( function(i) {
            $(this).tap( function(node) {
                
                var url = node.attr('data-orgurl');
                if (typeof(url) == 'undefined') {
                    url = node.attr('href');
                    node.attr('data-orgurl', url);
                }
                
                var splitUrl = url.split('#');
                if (splitUrl.length > 1) {
                    url = splitUrl[0];
                    var anchor = '#' + splitUrl[1];
                } else {
                    var anchor = '';
                }
                
                var params = $("#actionform").serializeArray();
                $.each(params, function(i, field) {
                    if (url.indexOf('?' > 0)) {
                        url += '&' + encodeURIComponent(field.name) + '=' + encodeURIComponent(field.value);
                    } else {
                        url += '?' + encodeURIComponent(field.name) + '=' + encodeURIComponent(field.value);
                    }
                });
                
                url += anchor;
                node.attr('href', url);
            });
        });
    }
    
    // forms
    if ($('.init_submit').length) {
        $('.init_submit').click( function(node) {
            $('#actionform').submit();
        });
    }
    
    if ($('.init_loc').length) {
        $('.init_loc').click( function () {
            
            getLocation( function(p) {
                $('input[name=longitude]').val(p.coords.longitude);
                $('input[name=latitude]').val(p.coords.latitude);
                $('#actionform').submit();
            }
            , function(p) {
                $('input[name=longitude]').val('error');
                $('input[name=latitude]').val('error');
                $('#actionform').submit();
            });
        });
    }
    
    // eek_popup
    if ($('.init_eek_popup_close').length) {
        $('.init_eek_popup_close').click( function() {
            $('.init_eek_popup').addClass('none');
            $('.init_eek_popup_bg').addClass('none');
        });
    }
    
    if ($('.init_nav_eek').length) {
        $('.init_nav_eek').click( function() {
            var eekid = '#eek_popup_' + $(this).attr('data-eekId');
            var eekPopUpid = '#eek_popup_bg_' + $(this).attr('data-eekId');
            var yOffset = window.pageYOffset;
            var screenH = window.innerHeight;
            var eekH = $(eekid).outerHeight();
            if (eekH >= screenH-20) {
                $(eekid).css('top', (yOffset + 20)  + 'px');
            } else {
                $(eekid).css('top', ((yOffset + screenH/2) - eekH/2)  + 'px');
            }
            $(eekid).removeClass('none');
            $(eekPopUpid).removeClass('none');
        });
    }
    
    // init slider
    $('.init_slider').each( function() {
        var slider = new Slider(this.id);
    });
    
    // switchboxes tab events
    if ($('.switchbox_a').length) {
        $('.switchbox_a').tap( function(node) {
            var handle = node.find('.switchbox_handle');
            var checkbox = node.find('input');
            if (checkbox.is(':checked')) {
                handle.css('left', '-1px');
                checkbox.removeAttr('checked');
            } else {
                handle.css('left', '37px');
                checkbox.attr('checked', 'checked');
            }
        }, {
            threshold: {x:300, y:2}
        });
    }
    
    // tabnav events
    if ($('.tab').length) {
        $('.tab').click( function() {
            var tab = $('.tab_a');
            
            tab.addClass('tab_i');
            tab.removeClass('tab_a');
            $(this).removeClass('tab_i');
            $(this).addClass('tab_a');
            
            $('#tab_content_' + tab.index()).addClass('none');
            $('#tab_content_' + $(this).index()).removeClass('none');
        });
        
    }
    
    // checkboxes
    if ($('.init_chk').length) {
        $('.init_chk').tap( function (node) {
            var ckbid = node.attr('data-chkid');
            var input = $('input[data-chkid="' + ckbid + '"]');
            if (input.is(':checked')) {
                input.removeAttr('checked');
            } else {
                input.attr('checked', 'checked');
            }
        });
    }
    
    // radiobuttons
    if ($('.init_radio').length) {
        $('.init_radio').tap(function (node) {
            var rdid = node.attr('data-rdid');
            var rdgroupid = rdid.split('_');
            var inputs = $('input[data-rdid="' + rdgroupid[0] + '*"]');
            inputs.each( function() {
                $(this).removeAttr('checked');
            });
            var input = $('input[data-rdid="' + rdid + '"]');
            input.attr('checked', 'checked');
        });
    }
    
    // gallery
    if ($('.swipe_gal').length) {
        $('.swipe_gal').swipe( function (node) {
            galNext(node);
        }, function (node) {
            galPrev(node);
        });
    }
    if ($('.tab_gal_next').length) {
        $('.tab_gal_next').tap( function (node) {
            galPrev($('.gal_image'));
        });
    }
    if ($('.tab_gal_prev').length) {
        $('.tab_gal_prev').tap( function (node) {
            galNext($('.gal_image'));
        });
    }
    
});

function galNext(gal) {
    var selected = gal.find('.inline_block');
    selected.addClass('none');
    selected.removeClass('inline_block');
    
    var next = selected.next();
    if (next.length == 0) {
        next = gal.children().first();
    }
    next.addClass('inline_block');
    next.removeClass('none');
}

function galPrev(gal) {
    var selected = gal.find('.inline_block');
    selected.addClass('none');
    selected.removeClass('inline_block');
    
    var prev = selected.prev();
    if (prev.length == 0) {
        prev = gal.children().last();
    }
    prev.addClass('inline_block');
    prev.removeClass('none');
}

function setOrientation() {
    var orient = Math.abs(window.orientation) === 90 ? 'landscape' : 'portrait';  
    var cl = document.body.className;
    cl = cl.replace(/portrait|landscape/, orient);
    document.body.className = cl;
}

function setPosition() {
    if (!window.location.hash) {
        setTimeout( function() {
            window.scrollTo(0, 1);
        }, 100);
    } else {
        setTimeout( function() {
            window.scrollBy(0,-70);
        }, 100);
    }
}
