var imagepath = '/img/standard/iphone';
    jQT = new $.jQTouch({
        addGlossToIcon: false,
        useAnimations: true,
        /*preloadImages: [
            imagepath+'activeButton.png',
            imagepath+'navi_more_off.png',
            imagepath+'add.png',
            imagepath+'bubbletwitter.png',
            imagepath+'navi_more_on.png',
            imagepath+'ajax-loader.gif',
            imagepath+'bubbleyellow.png',
            imagepath+'navi_my_off.png',
            imagepath+'arrow.png',
            imagepath+'bubbleyellow_active.png',
            imagepath+'navi_my_on.png',
            imagepath+'ask.png',
            imagepath+'button.png',
            imagepath+'navi_open_off.png',
            imagepath+'back_button.png',
            imagepath+'button_bg.png',
            imagepath+'navi_open_on.png',
            imagepath+'back_button_clicked.png',
            imagepath+'button_bg_active.png',
            imagepath+'navi_search_off.png',
            imagepath+'background.png',
            imagepath+'button_clicked.png',
            imagepath+'navi_search_on.png',
            imagepath+'bar.pngchevron.png',
            imagepath+'navi_topics_my.png',
            imagepath+'bar_negative.png',
            imagepath+'chevron_circle.png',
            imagepath+'navi_topics_off.png',
            imagepath+'bar_neutral.png',
            imagepath+'exclamation_mark.png',
            imagepath+'navi_topics_on.png',
            imagepath+'bar_positive.png',
            imagepath+'grayButton.png',
            imagepath+'new_question_dot.png',
            imagepath+'bg-navi-active.png',
            imagepath+'greenButton.png',
            imagepath+'notify.png',
            imagepath+'bg-navi.png',
            imagepath+'icon_comment.png',
            imagepath+'on_off.png',
            imagepath+'bg_icon_twitter.png',
            imagepath+'icon_comment_active.png',
            imagepath+'pag-left.png',
            imagepath+'bubble.png',
            imagepath+'icon_mail.png',
            imagepath+'pag-right.png',
            imagepath+'bubble_detail.png',
            imagepath+'icon_mail_active.png',
            imagepath+'photo.png',
            imagepath+'icon_sendphoto.png',
            imagepath+'position.png',
            imagepath+'icon_sendphoto_active.png',
            imagepath+'questionmark.png',
            imagepath+'bubble_theme.png',
            imagepath+'icon_sendposition.png',
            imagepath+'redButton.png'
        ]*/
});



var hiogiIphone = function(){
	
	
	this.beforeRender = function(page){
        try {
          var pageTracker = _gat._getTracker("UA-2556341-29");
          pageTracker._trackPageview();
        } catch(err) {}
      
		this.fixHeight();
		if (page.attr('id') == 'imageDetail') 
			this.resizeImg();
	}
	
	this.beforeSubmit = function(form){
        this.checkPlaceHolder(form);
		if (form.attr('id') == 'reviewForm') 
            this.setRating(form);

        			
    }
    
	
	this.fixHeight = function(){
		var height = window.innerHeight, orientation = window.innerWidth < height ? 'profile' : 'landscape', ftheight = 100
		$(".vertical-scroll.content").css('height', (height - 100) + 'px');
		$("#footer").css('top', (height + 2) + "px");
	}
	
	this.resizeImg = function(){
		$("#imageDetail img").css("max-height", parseInt($("#imageDetail div.content").css("height")) - 80);
		$("#imageDetail img").css("max-width", window.innerWidth);
	}
	
	this.setNavi = function(data){
		navi = $(data).find('#mainNavi').html();
		
		if (navi) {
			$('#footer ul li').removeClass('selected');
			$('.' + navi).addClass('selected');
		}		
		
		loggedIn = $(data).find('#loggedIn').html();
		if (loggedIn) 
			$('.login a').html(loggedIn);
		
		flash = $(data).find('#flash').html();
		if (flash) 
			alert(flash);
	}
	
	
	this.placeholder = function(e){
		var $this = $(this);
		if ($this.val() == $this.attr('placeholder')) {
			$this.val('');
		}
		return false;
	}
	
	this.checkPlaceHolder = function(form){
		if(form.find('textarea').attr('placeholder')==form.find('textarea').val())
            form.find('textarea').val('');
		return true;
    }
	
	this.setRating = function(form){
		$rating = form.find('.review.active');
        
		if ($rating.hasClass('positive')) 
			$('#rating').val('positive');
		else if ($rating.hasClass('neutral')) 
            $('#rating').val('neutral');
        else 
    		$('#rating').val('negative');
			
		return false;			
	}
	
	this.iphoneSwitchRequest = function(el) {
        $.get($(el).attr('url'), function(response){ 
	    	if (response == 'on') {
				$(el).attr('url',$(el).attr('url').replace('/add/', '/delete/'));
                $(el).parent().addClass('active');
			}
			else {
				$(el).attr('url',$(el).attr('url').replace('/delete/', '/add/'));
                $(el).parent().removeClass('active');

			}				
        });
    }   
	
    this.review = function(e){
		var $this = $(this), $votetetxt = $('#voteText')
		votetxtval = $votetetxt.val();
		
		$(".review").removeClass('active');
		$this.addClass('active');
		if (votetxtval == $votetetxt.attr('placeholder_optional') && $this.hasClass('negative')) {
			$votetetxt.val($votetetxt.attr('placeholder_obligatory'));
			$votetetxt.attr('placeholder', $votetetxt.attr('placeholder_obligatory'));
		}
		
		if (votetxtval == $votetetxt.attr('placeholder_obligatory') && ($this.hasClass('positive') || $this.hasClass('neutral'))) {
			$votetetxt.val($votetetxt.attr('placeholder_optional'));
			$votetetxt.attr('placeholder', $votetetxt.attr('placeholder_optional'));
		}
		
		return false;
	}
	
	
	
	this.validateText = function() {
        var $this   = $(this),
            $counter= $this.data('counter'),
            maxlen  = $this.data('maxlen'),
            val     = $this.val(),
            count   = val.length;
        
        if(!$counter) {
            $counter =$this.parent().parent().find("span.charcounter");
            $this.data('counter',$counter);
            maxlen = $counter.attr("rel");
            $this.data('maxlen',maxlen);    
            
        }   
        if(count>maxlen){
                $this.val(val.substring(0,maxlen));
                count=maxlen;
        }
        setTimeout(function(){$counter.html(count + "/"+maxlen);},100);
    }   
    
    
	/* events */
	$('textarea').live('keyup',this.validateText);
	$("#questiontext, #voteText, #commentText, #subjectForm, #messageForm").live('focus',this.placeholder);
    $(".review").live('click',this.review).live('touchstart',this.review);
	$("#footer, .toolbar, .content").unbind('touchmove').bind('touchmove', function(e){return false;});
	
	
 
}   

var hiogi = new hiogiIphone();
$(document).ready(function() {
	hiogi.setNavi('#jqt > div');
	window.scrollTo(0,1);
});



