
function addEvent(obj, evType, fn) {
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, true);
		return true;
	} else if (obj.attachEvent) {
		var r = obj.attachEvent("on"+evType, fn); 
		return r;
	} else {
		return false;
	}
}
function inputFocus() {
	if(this.value==this.title)this.value='';
}
function inputBlur() {
	if(this.value=='')this.value=this.title;
}
function inputs_prepopulate() {
	inputs = document.getElementsByTagName('input');
	n_inputs = inputs.length;
	for (i = 0; i < n_inputs; i++) {
			if ((inputs[i].type=='text') && (inputs[i].title!='')){
					inputs[i].value=inputs[i].title;
					inputs[i].onfocus=inputFocus;
					inputs[i].onblur=inputBlur;
			}
	}
}
function prepopulate() {
	inputs_prepopulate();
}

function showPic (whichpic) {
    if (document.getElementById) {
        document.getElementById('placeholder').src = whichpic.href;
        return false;
    } 
    else {
        return true;
    }
}

//extension
String.prototype.beginsWith = function (t, i) {
    if (i == false) {
        return(t == this.substring(0, t.length));
    } else {
        return (t.toLowerCase()
== this.substring(0, t.length).toLowerCase());
    } 
}

function postPopularValueForFaq(value, pageref, $anchor) {    
    $.getJSON("http://jsonip.appspot.com?callback=?", function (ipadd) {
        $.ajax({
            url: '/web/ui/handlers/getpopularvalueshandler.ashx',
            data: 'pageref=' + pageref + '&value=' + value + '&ip=' + ipadd.ip,
            success: function (responseText) {               
                if (responseText.beginsWith("SUCCESS_", true)) {
                    var value = responseText.substring(8);                    
                    var $parent = $anchor.parent();                    
                    $anchor.hide();
                    $anchor.siblings().hide();
                    $.ajax({
                        url: '/web/ui/handlers/translatehandler.ashx',
                        data: 'key=/templatepackage/templates/faq/popularvalue/voted' + value,
                        success: function (responseData) {
                            $parent.append("<span class=\"" + value + "\">" + responseData + "</span>");
                        }
                    });
                }
            },
            error:function(responseText) {
            	alert(responseText);
            }
        });
    });
    return false;
}

$(document).ready(function() {  
    $('#push').slide();
    /*if(typeof window.showMovie == 'function') {
	    showMovie();	
	}*/
	
	    //Faq
    $('.faq .faq-list li strong.faqheader').wrap('<a href="#"></a>');
    $('.faq .faq-list li').children("div").hide();
    $('.faq-list li').children('a').click(function () {
        if ($($(this).closest('li').children("div")).is(":hidden"))
            $(this).addClass('expanded');
        else
            $(this).removeClass('expanded');
        $(this).closest('li').children("div").slideToggle('normal');
        return false;
    });
	
	//Thumbs up FAQ list
    $('.faq-list a.like').click(function () {
        var url = $(this).attr('href').substring(0, $(this).attr('href').indexOf('?'));
        return postPopularValueForFaq(1, url, $(this));
    });
    $('.faq-list a.dislike').click(function () {
        var url = $(this).attr('href').substring(0, $(this).attr('href').indexOf('?'));
        return postPopularValueForFaq(0, url, $(this));
    });
	
	// Find all anchors with the class popup and add the onclick function to open the link in a new window.
    $('a[class*="popup"]').click(function() {
        window.open($(this).attr('href'));
        return false;
    });
	
    addCloseEvent();
    $('#main .inner #imagebank .images .img').click(function(){
        var loader = $("#loading"+$(this).attr("id"));
        loader.show();
        var currenthref = location.href.substring(0,location.href.lastIndexOf("/")+1);
            $.ajax({
                    url: '/web/ui/handlers/ImageDetailsHandler.ashx'+$(this).attr("href")+'&location=' + currenthref,
                    success: function (data) {
                       if( $('#main .inner #imagebank #imagedetails').length > 0 ) {
                           $('#main .inner #imagebank #imagedetails').remove();                                              
                       }
                       $('#main .inner #imagebank').append(data);                                              
                       addCloseEvent();
                       loader.hide();
                    },
                    cache: false
                });
            return false;
    });
});

function addCloseEvent(){
    $('#main .inner #imagebank #imagedetails .close').click(function(){
        $('#main .inner #imagebank #imagedetails').fadeOut('normal',function(){$(this).remove()});
        return false;
    });
}
