function focusToSearch(){
    if (document.searchFormImage){
        document.searchFormImage.query.focus();
    }
}

function modifyQuery(element) {
var elementName = element.name;
//alert(elementName)
//alert(document.searchFormImage.query.value)
var query = document.searchFormImage.query.value
if ( (query.match(elementName) == null) && (element.checked) )
    {
    //alert(query + elementName)
    document.searchFormImage.query.value = query + ' ' + elementName
    
    }
if ( (query.match(' '+elementName) != null) && (element.checked == false) )
    {
    //alert(query + elementName)
    document.searchFormImage.query.value = query.replace(' '+elementName,'')
    
    }
    
    
}


function changeCheckboxState(element) {
var name = element.name;
if ((name == 'single') && (document.searchFormImage.on_cd.checked))
    {
     document.searchFormImage.on_cd.checked = false;
     
    }

if ((name == 'on_cd') && (document.searchFormImage.single.checked))
    {
     document.searchFormImage.single.checked = false;
      
    }
}




function checkUncheckAll(theElement) {
	 //documentation for this script at http://www.shawnolson.net/a/693/
     var theForm = theElement.form, z = 0;
     var x = theForm.length;
    
     
    while (z < x)
    {
        if (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
        {
            
            theForm[z].checked = theElement.checked;
                        
        }
        else
        {
            
        }
    z++;
    }
    
}




function resizeImage(imageId, maxwidth, maxheight) {
    width = document.getElementById(imageId).width;
    height = document.getElementById(imageId).height;
    
    widthfactor = 1;
    heightfactor = 1;

    if (width > maxwidth) {
        widthfactor = (maxwidth / width);
    } 
    if (height > maxheight) {
        heightfactor = (maxheight / height);
    } 
    
    scale = Math.min(widthfactor, heightfactor);
    
    newwidth = Math.floor(width * scale);
    newheight = Math.floor(height * scale);

    document.getElementById(imageId).width = newwidth;
    document.getElementById(imageId).height = newheight;
    document.getElementById(imageId).style.visibility = 'visible';
}

function resizeThisImage(imgobj, maxwidth, maxheight) {
    
    width = imgobj.width;
    height = imgobj.height;
    
    widthfactor = 1;
    heightfactor = 1;

    if (width > maxwidth) {
        widthfactor = (maxwidth / width);
    } 
    if (height > maxheight) {
        heightfactor = (maxheight / height);
    } 
    
    scale = Math.min(widthfactor, heightfactor);
    if (scale < 1) {
        newwidth = Math.floor(width * scale);
        newheight = Math.floor(height * scale);
        imgobj.width = newwidth;
        imgobj.height = newheight;
    }
    imgobj.style.visibility = 'visible';
}

function getParentElementByTagName(obj, tagname) {
    tagname = tagname.toUpperCase(tagname);
    if (obj.nodeName == tagname)
        return obj;
    obj = obj.parentNode;
    if (obj == null)
        return null;
    return getParentElementByTagName(obj, tagname);
}

function getFirstElementByName(objlist, name) {
    for (var i=0; i<objlist.length; i++) {
        var child = objlist[i];
        if (! child.getAttribute)
            continue;
        if (child.getAttribute("name") == name)
            return objlist[i];          
    }
    return null;
}

function itemSubmitAction(formobj) {
    if (formobj.album_clicked) {
        formobj.submit();
        return false;
    }
}

function number_spelling(number, liczba, liczby, liczb) {
    if (number == 1)
        return liczba;
    last_digit = number % 10;
    if ((last_digit > 1) && (last_digit < 5))
        return liczby;
    return liczb;
}

function albumSubmit(obj) {
    if (obj.album_rename) {
        var new_name = prompt("Nowa nazwa albumu:","");
        if (new_name == null) {
            return false;
        }
        obj.rename_album_name.value = new_name;
        return true;
    }
    else if (obj.album_new) {
        var new_name = prompt("Nazwa nowego albumu:", obj.new_album_name.value);
        if (new_name == null) {
            return false;
        }
        obj.new_album_name.value = new_name;
        return true;
    }
    else if (obj.do_delete_album) {
        var delete_question = "Czy chcesz usun±ć ten album?";
        if (confirm(delete_question))
            return true;
        else
            return false;
    }

    else if (obj.do_transfer_album) {
        var target_username = prompt("Komu przekazać album (email):", '');
        if (target_username == null) {
            return false;
        }
        obj.transfer_album_target_name.value = target_username;
        return true;
    }
    return true;
}

function renameAlbum(input) {
    input.form.rename_album_name.value = '';
    input.form.album_rename = true;
}

function transferAlbum(input) {
    input.form.do_transfer_album = true;
    
}

function deleteAlbum(input) {
    input.form.do_delete_album = true;
}

function newAlbum(input) {
    input.form.album_new = true;
}

    
function hideAlbum() {
    var album_container = document.getElementById("recent_album_items");
    album_container.style.display = 'none';
    var me = document.getElementById("show_hide_album");
    me.value = '[pokaz album]';
    me.onclick = showAlbum;
}


function showAlbum() {
    var album_container = document.getElementById("recent_album_items");
    album_container.style.display = 'block';
    var me = document.getElementById("show_hide_album");
    me.value = '[ukryj album]';
    me.onclick = hideAlbum;
}

function addToAlbum(obj) {
    // find table enclosing passed object, clone the table
    var table = getParentElementByTagName(obj, "table");
    var cloned = table.cloneNode(true); // true == deep copy    

    // change input in cloned form to 'remove_product_from_album'
    var form = cloned.getElementsByTagName("form")[0];
    //var input = getFirstElementByName(form.childNodes, "add_product_to_album");
    var enclosingDivInClone = form.getElementsByTagName("div")[0];
    
    var input = getFirstElementByName(enclosingDivInClone.childNodes, "add_product_to_album");
    var foo = document.createElement("div");
    foo.innerHTML = '<input name="remove_product_from_album" src="/pl/theta/images/albumX2.gif" alt="usuń produkt z albumu" type="image" align="center" border="0" height="23" width="24">';
    var new_input = foo.getElementsByTagName("input")[0];
    //form.replaceChild(new_input, input);
    enclosingDivInClone.replaceChild(new_input, input);
    
    var img = cloned.getElementsByTagName("img")[0];
    resizeThisImage(img, 60, 60);
    //var td = cloned.getElementsByTagName("td")[0];
    //td.setAttribute("class", "smallitemcell");
    
    var album_container = document.getElementById("recent_album_items");
    var tbody = album_container.getElementsByTagName("tbody")[0];
    var tr = document.createElement("tr");
    var td = document.createElement("td");
    td.setAttribute("class", "smallitems");
    tr.appendChild(td);
    td.appendChild(cloned);
    tbody.appendChild(tr);
    
    //clicked_input = getFirstElementByName(obj.childNodes, "add_product_to_album");
    var enclosingDiv = obj.getElementsByTagName("div")[0];
    clicked_input = getFirstElementByName(enclosingDiv.childNodes, "add_product_to_album");
    clicked_input.style.visibility = 'hidden';
    clicked_input.style.width = 0;

    var foo = document.createElement("span");
    foo.innerHTML = '<span class="text_under_item_image">w albumie</span>';
    var span = foo.getElementsByTagName("span")[0];
    //obj.appendChild(span);
    enclosingDiv.appendChild(span);

/*    
    // change input to a text
    input = getFirstElementByName(obj.childNodes, "add_product_to_album");
    var foo = document.createElement("span");
    foo.innerHTML = "<span>w albumie y</span>";
    var span = foo.getElementsByTagName("span")[0];
    obj.replaceChild(span, input);
*/  
    document.album_len += 1;
    things = number_spelling(document.album_len, 'rzecz', 'rzeczy', 'rzeczy');
    things = document.album_len + ' ' + things
    document.getElementById("things_in_album").innerHTML = things
    return true; // ensures the form will be submitted

    //alert(album_container.innerHTML);
    //alert(tag.toString())
}


function addToAlbumFromPopUp(obj) {
    clicked_input = getFirstElementByName(obj.childNodes, "add_product_to_album");
    clicked_input.style.visibility = 'hidden';
    clicked_input.style.width = 0;

    var foo = document.createElement("span");
    // foo.innerHTML = '<span class="text_under_item_image">w albumie</span>';
    foo.innerHTML = '<span>w albumie</span>';
    var span = foo.getElementsByTagName("span")[0];
    obj.appendChild(span);
    return true;

}

function addToCartFromPopUp(obj) {
    clicked_input = getFirstElementByName(obj.childNodes, "add_product_to_cart");
    clicked_input.style.visibility = 'hidden';
    clicked_input.style.width = 0;

    var foo = document.createElement("span");
    // foo.innerHTML = '<span class="text_under_item_image">w koszyku</span>';
    foo.innerHTML = '<span>w koszyku</span>';
    var span = foo.getElementsByTagName("span")[0];
    obj.appendChild(span);
    return true;

}

//popupy na stronie Animation Factory
function tour_pop (page, arg1) {
   var tp = window.open('http://www.animationfactory.com/en/tour/' + page + '?pg=' + arg1,
   'preview_pop',
   'width=600,height=525,scrollbars=yes,resizable=yes,menubar=no');
    tp.focus();
}

// dodanie wyszukiwarki Theta
function AddFirefoxPlugin()
{
	try
	{
		window.sidebar.addSearchEngine('http://www.theta.pl/static/theta.src', 'http://www.theta.pl/static/favicon.ico', 'Theta', '');
	}
	catch(e)
	{
		alert('This feature requires Firefox.\n\nYou can get it at http://www.getfirefox.com');
	}
}

function AddIE7Plugin()
{
	try
	{
		window.external.AddSearchProvider('http://www.theta.pl/static/theta-ie7search.xml');
	}
	catch(e)
	{
		alert('This feature requires Internet Explorer 7.');
	}
}

// ********************************************************* 
 

function getSearchURLInSubs(prefix,query) {
   //query is quoted to avoid errors with space between keywords
   return prefix + 'szukaj/index_html?query="' + query + '"&color=1&color=0&orientation=1&orientation=2&orientation=3&orientation=4&action_szukaj_zdjecia=true&page=1&pageSize=20&action_szukaj_zdjecia=%5Bszukaj%5D';
   }

function setSearchForKeywordInSubs(prefix,keyword) {
    
    if (self.opener != null) {
       self.close();
       self.opener.location.href= getSearchURLInSubs(prefix,keyword);
       
       
    }
    else {
       
       self.location.href= getSearchURLInSubs(prefix,keyword);
    }
}

function getSearchURL(prefix,query) {
   //query is quoted to avoid errors with space between keywords
   return prefix + 'szukaj?query="' + query + '"&action_szukaj_zdjecia=%5Bszukaj%5D';
   }

function getSearchURLForFilms(prefix,query) {
   //query is quoted to avoid errors with space between keywords
   return prefix + 'szukaj?query="' + query + '"&action_szukaj_filmu=%5Bszukaj%5D';
   }

function setSearchForKeyword(prefix,keyword) {
    if (self.opener != null) {
       self.close();
       self.opener.location.href= getSearchURL(prefix,keyword);
       
       
    }
    else {
       //alert('aaa..aa teraz zrobimy searcha! url:'+keyword)
       self.opener.location.href= getSearchURL(prefix,keyword);
    }
}

function setSearchForKeywordForFilms(prefix,keyword) {
    if (self.opener != null) {
       self.close();
       self.opener.location.href= getSearchURLForFilms(prefix,keyword);
       
       
    }
    else {
       //alert('aaa..aa teraz zrobimy searcha! url:'+keyword)
       self.opener.location.href= getSearchURLForFilms(prefix,keyword);
    }
}

function goToHomePage() {
    if (self.opener != null) {
       self.close();       
       self.opener.goToHomePage();
       
       
    }
    else {
       self.location.href='http://www.theta.pl/pl/theta/szukaj';
    }
}

function closeAllgoToURL(URL) {
    //alert(URL)
    if (self.opener != null) {
       self.close();       
       self.opener.closeAllgoToURL(URL);
       
       
    }
    else {
       //alert(URL)
       self.location.href='http://www.theta.pl/pl/theta/szukaj?name=&category='+URL+'&producer_id%3Alist=&action_szukaj_plyty=true&action_szukaj_plyty=%5Bszukaj%5D';
       
       //self.location.href='http://www.theta.pl/pl/theta/szukaj';
    }
}

function displayWindow(url, width, height) {
    
    var detailsWindow = window.open(url,"details_window",
        'width=' + width + ',height=' + height + 
        ',resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes' );
    window.name = 'theta';
    detailsWindow.name = 'imagePopUp';
    
    if (window.focus) {detailsWindow.focus()};
    if (!detailsWindow.creator) {
        // just created, save reference to creator
    }
}

function displayCD(url, width, height) {
    var detailsCDWindow = window.open(url,"details_cd_window",
        'width=' + width + ',height=' + height + 
        ',resizable=yes,scrollbars=yes,menubar=yes' );
    
    detailsCDWindow.name = 'CDPopUp';
    detailsCDWindow.moveBy(20,20);
    if (window.focus) {detailsCDWindow.focus()};
}

function displaySubscription(url, width, height) {
    
    var subscriptionWindow = window.open(url,"subscription_window",
        'width=' + width + ',height=' + height + 
        ',resizable=yes,scrollbars=yes,menubar=yes,toolbar=yes' );
    window.name = 'theta';
    subscriptionWindow.name = 'subscriptionPopUp';
    
    if (window.focus) {subscriptionWindow.focus()};
    if (!subscriptionWindow.creator) {
        // just created, save reference to creator
    }
}

function submitOnReturn(event, buttonName, formName) {
    if ((event.which && event.which == 13) || (event.keyCode && event.keyCode == 13)) {
        document[formName][buttonName].click();
        return false;
        } 
    else return true; }

