// JavaScript Document
/**
 * Created by IntelliJ IDEA.
 * User: 
 * Date: 
 * Time: 
*/


jQuery(document).ready(function() {

    jQuery('img.background-image').each(function() {
        setBackgroundImageWidth(jQuery(this));
    });


});
jQuery(window).load(function() {

    jQuery('img.background-image').each(function() {
        setBackgroundImageWidth(jQuery(this));
    });
    jQuery('img.background-image').fadeIn("fast");
});

jQuery(window).resize(function() {
    jQuery('img.background-image').each(function() {
        setBackgroundImageWidth(jQuery(this));
    });
});

if(isiPhone()){
    jQuery('img.background-image').live('touchstart', function() {
        //jQuery(this).css({ top: (scroll_y) + "px" });   
    });

    jQuery('.pagewrapper').live('touchmove', function(){
        //jQuery('img.background-image').css({ top: ($(window).scrollTop()) + "px" });
    });

    jQuery(window).scroll(function(){
        var scroll_y = $(window).scrollTop();
        jQuery('img.background-image').each(function() {
            jQuery(this).css({ top: (scroll_y) + "px" });
        });
    });
}

function isiPhone(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1) ||
        (navigator.platform.indexOf("iPad") != -1)
    );
}

function setBackgroundImageWidth(target) {
    var windowWidth = jQuery(window).width();
    var windowHeight = jQuery(window).height();
    var pageHeight = windowHeight;
    if(isiPhone()){
       jQuery('img.background-image').each(function() {
            jQuery(this).css({ top: 0+ "px" });
        });
    }

    var targetHeight = Math.max(windowHeight, pageHeight);
    jQuery(target).width(windowWidth);

    var thisHeight = jQuery(target).height();
    var ratio = thisHeight / targetHeight;

    if (ratio < 1) {
        jQuery(target).width(windowWidth / ratio);
    }
}

